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 {
if (keyStreamIndex < 0) {
throw ArgumentError.value(
keyStreamIndex,
'keyStreamIndex',
'Must be non-negative',
);
}
nonce ??= newNonce();
if (nonce.length != 12) {
throw ArgumentError.value(
nonce,
'nonce',
'Nonce must have 12 bytes',
);
}
final macAlgorithm = this.macAlgorithm;
if (macAlgorithm is DartChacha20Poly1305AeadMacAlgorithm) {
if (keyStreamIndex != 0) {
throw ArgumentError.value(
keyStreamIndex,
'keyStreamIndex',
'Must be zero',
);
}
keyStreamIndex = 64;
}
final secretKeyData = await secretKey.extract();
if (secretKeyData.bytes.length != 32) {
throw ArgumentError.value(
secretKey,
'secretKey',
'length must be 32 bytes',
);
}
final cipherText = _xorSync(
clearText,
secretKey: 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,
);
}