decryptSerializedStringWithKey method
Pass a string in Cryppo serialized encrypted format and a SymmetricKey (key type dependant on the scheme being used) to return binary decrypted data.
Implementation
@override
Future<Uint8List> decryptSerializedStringWithKey(
String serialized, EncryptionKey key) {
assert(key is SymmetricKey, 'AES requires a `SymmetricKey`');
final items = serialized.split('.');
final decodedPairs = items.sublist(1);
final encryptedBytes = base64Url.decode(decodedPairs[0]);
final encryptionArtefacts =
EncryptionArtefacts.fromSerialized(decodedPairs[1]);
// The `cryptography` library takes the last 16 bytes as the auth tag
// so we need to concatenate them onto the cipher text.
final fullCipher = Uint8List(encryptedBytes.length + _authTagLength);
fullCipher.setAll(0, encryptedBytes);
fullCipher.setAll(encryptedBytes.length, encryptionArtefacts.authTag);
final mac = Mac(encryptionArtefacts.authTag);
final secretBox =
SecretBox(encryptedBytes, nonce: encryptionArtefacts.nonce, mac: mac);
return _cipher
.decrypt(
secretBox,
aad: encryptionArtefacts.authData,
secretKey: SecretKey((key as SymmetricKey).bytes),
)
.then((value) => Uint8List.fromList(value));
}