Initial project

This commit is contained in:
Cody
2026-03-06 14:56:17 +08:00
parent 977b627b15
commit bf9e099747
1180 changed files with 50973 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
/// SDK API
/// 端對端加密的統一入口
library;
import 'package:cipher_guard_sdk/src/domain/entities/rsa_key_pair.dart';
import 'package:cipher_guard_sdk/src/domain/entities/session_key.dart';
import 'package:cipher_guard_sdk/src/domain/entities/encrypted_message.dart';
import 'package:cipher_guard_sdk/src/presentation/wiring/cipher_guard_sdk_wiring.dart';
abstract class CipherGuardSdkApi
{
factory CipherGuardSdkApi() => CipherGuardSdkWiring.build();
// ==================== 平台版本 ====================
/// 獲取平台版本
Future<String?> platformVersion();
// ==================== RSA 金鑰管理 ====================
/// 生成 RSA 金鑰對
Future<RsaKeyPair> generateRsaKeyPair({int keySize = 1024});
/// 用密碼加密私鑰
Future<String> encryptPrivateKey({required String privateKey, required String password,});
/// 解密私鑰
Future<String> decryptPrivateKey({required String encryptedPrivateKey, required String password,});
// ==================== 會話金鑰管理 ====================
/// 生成 AES 會話金鑰
Future<SessionKey> generateSessionKey({int initialRound = 1});
/// 用 RSA 公鑰加密會話金鑰
Future<String> encryptSessionKey({required String sessionKey, required String publicKey,});
/// 用 RSA 私鑰解密會話金鑰
Future<String> decryptSessionKey({required String encryptedSessionKey, required String privateKey,});
// ==================== 訊息加解密 ====================
/// 加密訊息
Future<EncryptedMessage> encryptMessage({required String plaintext, required String sessionKey, required int round,});
/// 解密訊息
Future<String> decryptMessage({required String encryptedData, required String sessionKey, required int round,});
// ==================== 原生平台同步 ====================
/// 同步加密金鑰到原生平台 (iOS App Group)
Future<void> syncEncryptionKey({required String chatId, required int activeRound, required int round, required String activeKey, required bool isSingle,});
/// 批量同步所有加密聊天室的金鑰
Future<void> syncAllEncryptionKeys({required Map<String, Map<String, dynamic>> chatMap,});
// ==================== 推送通知解密 ====================
/// 設置 AES_SECRET (用於推送解密)
Future<void> setAesSecret({required String aesSecret});
/// 解密 APNS 推送通知內容
Future<String?> decryptPushNotification({required String encryptedData,});
}

View File

@@ -0,0 +1,131 @@
import 'package:cipher_guard_sdk/src/presentation/facade/cipher_guard_sdk_api.dart';
import 'package:cipher_guard_sdk/src/presentation/wiring/cipher_guard_sdk_core.dart';
import 'package:cipher_guard_sdk/src/domain/entities/rsa_key_pair.dart';
import 'package:cipher_guard_sdk/src/domain/entities/session_key.dart';
import 'package:cipher_guard_sdk/src/domain/entities/encrypted_message.dart';
/// SDK API 实现
class CipherGuardSdkApiImpl implements CipherGuardSdkApi {
final CipherGuardSdkCore _core;
CipherGuardSdkApiImpl({required CipherGuardSdkCore core}) : _core = core;
@override
Future<String?> platformVersion() => _core.platform.getPlatformVersion();
@override
Future<RsaKeyPair> generateRsaKeyPair({int keySize = 1024}) {
return _core.encryptionRepo.generateRsaKeyPair(keySize: keySize);
}
@override
Future<String> encryptPrivateKey({
required String privateKey,
required String password,
}) {
return _core.encryptionRepo.encryptPrivateKey(
privateKey: privateKey,
password: password,
);
}
@override
Future<String> decryptPrivateKey({
required String encryptedPrivateKey,
required String password,
}) {
return _core.encryptionRepo.decryptPrivateKey(
encryptedPrivateKey: encryptedPrivateKey,
password: password,
);
}
@override
Future<SessionKey> generateSessionKey({int initialRound = 1}) {
return _core.encryptionRepo.generateSessionKey(initialRound: initialRound);
}
@override
Future<String> encryptSessionKey({
required String sessionKey,
required String publicKey,
}) {
return _core.encryptionRepo.encryptSessionKey(
sessionKey: sessionKey,
publicKey: publicKey,
);
}
@override
Future<String> decryptSessionKey({
required String encryptedSessionKey,
required String privateKey,
}) {
return _core.encryptionRepo.decryptSessionKey(
encryptedSessionKey: encryptedSessionKey,
privateKey: privateKey,
);
}
@override
Future<EncryptedMessage> encryptMessage({
required String plaintext,
required String sessionKey,
required int round,
}) {
return _core.encryptionRepo.encryptMessage(
plaintext: plaintext,
sessionKey: sessionKey,
round: round,
);
}
@override
Future<String> decryptMessage({
required String encryptedData,
required String sessionKey,
required int round,
}) {
return _core.encryptionRepo.decryptMessage(
encryptedData: encryptedData,
sessionKey: sessionKey,
round: round,
);
}
@override
Future<void> syncEncryptionKey({
required String chatId,
required int activeRound,
required int round,
required String activeKey,
required bool isSingle,
}) {
return _core.encryptionRepo.syncEncryptionKeysToNative(
chatId: chatId,
activeRound: activeRound,
round: round,
activeKey: activeKey,
isSingle: isSingle,
);
}
@override
Future<void> syncAllEncryptionKeys({
required Map<String, Map<String, dynamic>> chatMap,
}) {
return _core.encryptionRepo.syncAllEncryptionKeys(chatMap: chatMap);
}
@override
Future<void> setAesSecret({required String aesSecret}) {
return _core.encryptionRepo.setAesSecret(aesSecret: aesSecret);
}
@override
Future<String?> decryptPushNotification({
required String encryptedData,
}) {
return _core.encryptionRepo.decryptPushNotification(encryptedData: encryptedData);
}
}

View File

@@ -0,0 +1,19 @@
import 'package:cipher_guard_sdk/src/domain/repositories/encryption_repository.dart';
/// SDK Core
/// 持有所有核心依賴
class CipherGuardSdkCore {
final EncryptionRepository encryptionRepo;
final CipherGuardPlatform platform;
CipherGuardSdkCore({
required this.encryptionRepo,
required this.platform,
});
}
/// 平台介面 (用於原生通訊)
abstract class CipherGuardPlatform {
Future<String?> getPlatformVersion();
}

View File

@@ -0,0 +1,42 @@
import 'package:cipher_guard_sdk/src/presentation/facade/cipher_guard_sdk_api.dart';
import 'package:cipher_guard_sdk/src/presentation/wiring/cipher_guard_sdk_core.dart';
import 'package:cipher_guard_sdk/src/data/datasources/encryption_flutter_service.dart';
import 'package:cipher_guard_sdk/src/data/repositories/encryption_repository_impl.dart';
import 'package:cipher_guard_sdk/src/presentation/wiring/cipher_guard_sdk_api_impl.dart';
/// SDK 依賴注入容器
/// 負責組裝所有依賴
/// 使用 Flutter 本地加密服務,無需原生平台處理加密邏輯
class CipherGuardSdkWiring {
/// 構建 SDK 實例
static CipherGuardSdkApi build() {
// 1. 創建 Flutter 加密服務
final flutterService = EncryptionFlutterService();
// 2. 創建 Repository (使用 Flutter 服務)
final repository = EncryptionRepositoryImpl(flutterService);
// 3. 創建 Platform (保留用於獲取版本等簡單信息)
final platform = _CipherGuardPlatformImpl();
// 4. 創建 Core
final core = CipherGuardSdkCore(
encryptionRepo: repository,
platform: platform,
);
// 5. 返回 API 實作
return CipherGuardSdkApiImpl(core: core);
}
}
/// Platform 實作
class _CipherGuardPlatformImpl implements CipherGuardPlatform {
_CipherGuardPlatformImpl();
@override
Future<String?> getPlatformVersion() async {
return 'Flutter Native'; // 所有加密邏輯現在都在 Flutter 端執行
}
}