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();
// New secret key for normal Chacha20
final derivedSecretKey = await _xchacha20SecretKey(
secretKey: secretKey,
nonce: nonce,
);
// New nonce for normal Chacha20
final derivedNonce = _xchacha20Nonce(nonce);
// Encrypt
final secretBox = await _chacha20.encrypt(
clearText,
secretKey: derivedSecretKey,
nonce: derivedNonce,
aad: aad,
keyStreamIndex: keyStreamIndex,
);
// New secret box
return SecretBox(
secretBox.cipherText,
nonce: nonce,
mac: secretBox.mac,
);
}