encrypt method
Future<SecretBox>
encrypt(
- List<
int> clearText, { - required SecretKey secretKey,
- List<
int> ? nonce, - List<
int> aad = const <int>[], - int keyStreamIndex = 0,
override
Encrypts a cleartext.
Parameter keyStreamIndex
allows you to choose offset in the keystream.
For other arguments, see Cipher.encrypt.
Implementation
@override
Future<SecretBox> encrypt(
List<int> clearText, {
required SecretKey secretKey,
List<int>? nonce,
List<int> aad = const <int>[],
int keyStreamIndex = 0,
}) async {
nonce ??= newNonce();
final secretKeyData = await secretKey.extract();
final actualSecretKeyLength = secretKeyData.bytes.length;
final expectedSecretKeyLength = secretKeyLength;
if (actualSecretKeyLength != expectedSecretKeyLength) {
throw ArgumentError.value(
secretKey,
'secretKey',
'Expected $secretKeyLength bytes, got $actualSecretKeyLength bytes',
);
}
final cipherText = _perform(
clearText,
secretKeyData: secretKeyData,
nonce: nonce,
aad: aad,
keyStreamIndex: keyStreamIndex,
);
final mac = await macAlgorithm.calculateMac(
cipherText,
secretKey: secretKey,
nonce: nonce,
aad: aad,
);
return SecretBox(cipherText, nonce: nonce, mac: mac);
}