decrypt method

  1. @override
Future<List<int>> decrypt(
  1. SecretBox secretBox, {
  2. required SecretKey secretKey,
  3. List<int> aad = const <int>[],
  4. int keyStreamIndex = 0,
})
override

Decrypts a ciphertext.

Parameter keyStreamIndex allows you to choose offset in the keystream.

For other arguments, see Cipher.decrypt.

Implementation

@override
Future<List<int>> decrypt(
  SecretBox secretBox, {
  required SecretKey secretKey,
  List<int> aad = const <int>[],
  int keyStreamIndex = 0,
}) async {
  // Secret key for normal Chacha20
  final derivedSecretKey = await _xchacha20SecretKey(
    secretKey: secretKey,
    nonce: secretBox.nonce,
  );

  // Nonce for normal Chacha20
  final derivedNonce = _xchacha20Nonce(secretBox.nonce);

  // New secret box
  final derivedSecretBox = SecretBox(
    secretBox.cipherText,
    nonce: derivedNonce,
    mac: secretBox.mac,
  );

  // Decrypt
  final clearText = await _chacha20.decrypt(
    derivedSecretBox,
    secretKey: derivedSecretKey,
    aad: aad,
    keyStreamIndex: keyStreamIndex,
  );

  return clearText;
}