decrypt method
Decrypts the provided data using the public key.
Implementation
@override
Future<Uint8List> decrypt(
Uint8List ivAndBytes, {
Uint8List? publicKey,
}) async {
// Extract the ephemeral public key and the encrypted data
final ephemeralPublicKeyBytes = ivAndBytes.sublist(
0,
compressedPublicKeyLength,
);
final encryptedData = ivAndBytes.sublist(
compressedPublicKeyLength,
); // The rest is the encrypted data
Uint8List pubKeyToUse;
if (publicKey == null) {
pubKeyToUse = ephemeralPublicKeyBytes;
} else {
pubKeyToUse = publicKey;
}
final sharedSecret = await computeEcdhSecret(pubKeyToUse);
final algorithm = crypto.Hkdf(hmac: crypto.Hmac.sha256(), outputLength: 32);
final secretKey = crypto.SecretKey(sharedSecret);
final derivedKey = await algorithm.deriveKey(
secretKey: secretKey,
nonce: staticHkdNonce,
);
final derivedKeyBytes = await derivedKey.extractBytes();
var symmetricKey = Uint8List.fromList(derivedKeyBytes);
final decryptedData = _encryptionUtils.decryptFromBytes(
symmetricKey,
encryptedData,
);
if (decryptedData == null) {
throw SsiException(
message: 'Decryption failed, bytes are null',
code: SsiExceptionType.unableToDecrypt.code,
);
}
return decryptedData;
}