checkParameters method

void checkParameters({
  1. int? length,
  2. required SecretKey secretKey,
  3. required int nonceLength,
  4. int aadLength = 0,
  5. int keyStreamIndex = 0,
})
inherited

Checks parameters for encrypt / decrypt and throws ArgumentError if any is invalid.

Implementation

void checkParameters({
  int? length,
  required SecretKey secretKey,
  required int nonceLength,
  int aadLength = 0,
  int keyStreamIndex = 0,
}) {
  if (secretKey is SecretKeyData) {
    final secretKeyLength = secretKey.bytes.length;
    final expectedSecretKeyLength = this.secretKeyLength;
    if (secretKeyLength != expectedSecretKeyLength) {
      throw ArgumentError(
        '$this expects a secret key with $expectedSecretKeyLength bytes, got $secretKeyLength bytes',
      );
    }
  }
  final expectedNonceLength = this.nonceLength;
  if (nonceLength != expectedNonceLength) {
    throw ArgumentError(
      '$this expects a nonce with $expectedNonceLength bytes, got $nonceLength bytes',
    );
  }
  if (aadLength != 0 && !macAlgorithm.supportsAad) {
    throw ArgumentError(
      '$this does not support AAD',
    );
  }
  if (keyStreamIndex != 0 && !macAlgorithm.supportsKeyStreamIndex) {
    throw ArgumentError.value(
      keyStreamIndex,
      'keyStreamIndex',
      '$this does not support key stream index',
    );
  }
  macAlgorithm.checkParameters(
    length: length,
    secretKey: secretKey,
    nonceLength: nonceLength,
    aadLength: aadLength,
    keyStreamIndex: keyStreamIndex,
  );
}