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 {
  // Validate arguments
  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',
    );
  }
  if (keyStreamIndex < 0) {
    throw ArgumentError.value(
      keyStreamIndex,
      'keyStreamIndex',
    );
  }

  // Authenticate
  await secretBox.checkMac(
    macAlgorithm: macAlgorithm,
    secretKey: secretKeyData,
    aad: aad,
  );

  return _perform(
    secretBox.cipherText,
    secretKeyData: secretKeyData,
    nonce: secretBox.nonce,
    aad: aad,
    keyStreamIndex: keyStreamIndex,
  );
}