decryptToBytes method
Implementation
Future<List<int>> decryptToBytes(String envelopeString, String passphrase) async {
if (type == CryptoType.none) {
// No encryption: interpret envelopeString as base64 of raw bytes
try {
final bytes = base64Decode(envelopeString);
_log(LogLevel.debug, 'DecryptBytes none base64 -> len=${bytes.length}');
return bytes;
} catch (_) {
// If not base64, maybe it was plaintext string -> return UTF-8 bytes
final bytes = await JsonIsolates.utf8Encode(envelopeString);
_log(LogLevel.debug, 'DecryptBytes none plaintext -> len=${bytes.length}');
return bytes;
}
}
try {
// Parse envelope JSON in an isolate
final j = await JsonIsolates.decodeMap(envelopeString);
final s = base64Decode(j['salt']);
final n = base64Decode(j['nonce']);
final c = base64Decode(j['ciphertext']);
final m = Mac(base64Decode(j['mac']));
final ti = type.index - 1;
final kb = const [128, 256, 256][ti];
final k = await _deriveKey(passphrase, s, bits: kb);
final sb = SecretBox(c, nonce: n, mac: m);
final algos = [_aesGcm128, _aesGcm256, _chacha20];
final algo = algos[ti];
final x = await algo.decrypt(sb, secretKey: k);
_log(LogLevel.debug, 'DecryptBytes ok len=${x.length}');
return x;
} catch (e) {
throw GitStorageException('Erro ao descriptografar: $e');
}
}