117 lines
3.6 KiB
Dart
117 lines
3.6 KiB
Dart
///
|
|
///
|
|
///
|
|
import 'ccc_iso_manager.dart';
|
|
import 'ccc_iso_result.dart';
|
|
import 'cipher_constants.dart';
|
|
|
|
/// Copious Cipher Chain - Phase 1 with Isolate Support
|
|
/// Now supports async operations using CryptoIsolateManager
|
|
class CopiousCipherChain {
|
|
final CryptoIsolateManager? _isolateManager; // New isolate-based crypto
|
|
final List<int> _cipherSequence; // Integer-based cipher sequence
|
|
final Map<String, dynamic> _cipherParams; // Cipher parameters
|
|
|
|
/// New constructor with isolate manager (Phase 1)
|
|
CopiousCipherChain.withIsolateManager({
|
|
required CryptoIsolateManager isolateManager,
|
|
List<int>? cipherSequence,
|
|
Map<String, dynamic>? cipherParams,
|
|
}) : _isolateManager = isolateManager,
|
|
_cipherSequence = cipherSequence ?? CipherConstants.PHASE1_SEQUENCE,
|
|
_cipherParams = cipherParams ?? CipherConstants.DEFAULT_CIPHER_PARAMS;
|
|
|
|
/// Async encrypt with isolate support
|
|
Future<List<int>> encryptAsync(
|
|
List<int> data, {
|
|
required String channelUuid,
|
|
int? messageSequence,
|
|
bool isUserMessage = false,
|
|
List<int>? customCipherSequence,
|
|
Map<String, dynamic>? customParams,
|
|
}) async {
|
|
if (_isolateManager == null) {
|
|
throw StateError('Isolate manager not available. Use legacy encrypt() or create with withIsolateManager()');
|
|
}
|
|
|
|
final result = await _isolateManager.encrypt(
|
|
data,
|
|
channelUuid: channelUuid,
|
|
messageSequence: messageSequence,
|
|
cipherSequence: customCipherSequence ?? _cipherSequence,
|
|
params: customParams ?? _cipherParams,
|
|
isUserMessage: isUserMessage,
|
|
);
|
|
|
|
if (!result.success) {
|
|
throw Exception('Encryption failed: ${result.error}');
|
|
}
|
|
|
|
return result.data;
|
|
}
|
|
|
|
/// Async decrypt with isolate support
|
|
Future<List<int>> decryptAsync(
|
|
List<int> data, {
|
|
required String channelUuid,
|
|
int? messageSequence,
|
|
List<int>? customCipherSequence,
|
|
Map<String, dynamic>? customParams,
|
|
}) async {
|
|
if (_isolateManager == null) {
|
|
throw StateError('Isolate manager not available. Use legacy decrypt() or create with withIsolateManager()');
|
|
}
|
|
|
|
final result = await _isolateManager.decrypt(
|
|
data,
|
|
channelUuid: channelUuid,
|
|
messageSequence: messageSequence,
|
|
cipherSequence: customCipherSequence ?? _cipherSequence,
|
|
params: customParams ?? _cipherParams,
|
|
);
|
|
|
|
if (!result.success) {
|
|
throw Exception('Decryption failed: ${result.error}');
|
|
}
|
|
|
|
return result.data;
|
|
}
|
|
|
|
/// Test the cipher chain with a roundtrip operation
|
|
Future<bool> testCipherChain({
|
|
String testData = 'Test message for cipher chain validation',
|
|
String channelUuid = 'test-channel-999',
|
|
}) async {
|
|
if (_isolateManager == null) {
|
|
return false; // Cannot test without isolate manager
|
|
}
|
|
|
|
try {
|
|
final result = await _isolateManager.testOperation(
|
|
testData: testData,
|
|
channelUuid: channelUuid,
|
|
);
|
|
return result.success;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Get cipher sequence description
|
|
String get cipherSequenceDescription {
|
|
return CipherConstants.getSequenceDescription(_cipherSequence);
|
|
}
|
|
|
|
/// Get performance metrics (if using isolate manager)
|
|
CryptoMetrics? get metrics => _isolateManager?.metrics;
|
|
|
|
/// Check if using isolate manager
|
|
bool get usesIsolateManager => _isolateManager != null;
|
|
|
|
/// Get current cipher sequence
|
|
List<int> get cipherSequence => List.from(_cipherSequence);
|
|
|
|
/// Get current cipher parameters
|
|
Map<String, dynamic> get cipherParams => Map.from(_cipherParams);
|
|
}
|