dint-dev / cryptography

Cryptography for Flutter developers: encryption, digital signatures, key agreement, etc.
https://pub.dev/packages/cryptography
Apache License 2.0
162 stars 90 forks source link

Argument Error: aad not supported, but it is a required argument #157

Open k98kurz opened 1 year ago

k98kurz commented 1 year ago

Trying to use this package in Flutter Flow, but I get an ArgumentError when the secret key is invalid. The code is below.

Future<String?> decryptString(String ciphertext, String secretKeyB64) async {
  if (ciphertext.split(';').length != 3) {
    return null;
  }
  final secretKey = SecretKey(convert.Base64Decoder().convert(secretKeyB64));
  final algo = Chacha20(macAlgorithm: Hmac.sha256());
  var [nonceStr, ctStr, macStr] = ciphertext.split(';');
  final nonce = convert.Base64Decoder().convert(nonceStr);
  final ct = convert.Base64Decoder().convert(ctStr);
  final mac = convert.Base64Decoder().convert(macStr);
  final box = SecretBox(ct, nonce: nonce, mac: Mac(mac));

  try {
    await box
        .checkMac(macAlgorithm: Hmac.sha256(), secretKey: secretKey, aad: []);
  } on Exception {
    return null;
  } on ArgumentError {
    return null;
  }
  final pt = await algo.decrypt(box, secretKey: secretKey);
  return convert.Utf8Decoder().convert(pt);
}

When I run similar code from the console and supply the wrong key, it instead throws an authentication error.

Not sure if there is a versioning issue with the dependencies or just a platform difference, but it does not make sense to require an argument and then not support it.