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

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

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