aesGcmEncrypt function
AES-256-GCM encrypt. Output layout: ciphertext + 16-byte tag.
aad is the additional authenticated data nosqueel supplies to bind the blob
to its on-disk domain. It is authenticated by the GCM tag but not encrypted,
and the same bytes must be passed to aesGcmDecrypt for the tag to verify.
Implementation
Uint8List? aesGcmEncrypt(Uint8List key, Uint8List plaintext, Uint8List aad) {
if (key.length != _aesKeySize) {
return null;
}
final nonce = _secureNonce(_nonceSize);
final cipher = GCMBlockCipher(AESEngine())
..init(
true,
AEADParameters(KeyParameter(key), _macBits, nonce, aad),
);
final encrypted = cipher.process(plaintext);
final output = Uint8List(_nonceSize + encrypted.length);
output.setRange(0, _nonceSize, nonce);
output.setRange(_nonceSize, output.length, encrypted);
return output;
}