decode static method

String decode(
  1. String content,
  2. String secret, {
  3. String associatedData = '',
  4. String purpose = KeyDerivation.encrypt,
  5. List<String> previousSecrets = const <String>[],
})

decode a message. Throws DecryptException when the text was changed or the key is wrong. When secret fails, each of previousSecrets is tried (old keys, see AppConfig.previousKeys).

AESEncryptor.decode('encoded_message', secret);

Implementation

static String decode(
  String content,
  String secret, {
  String associatedData = '',
  String purpose = KeyDerivation.encrypt,
  List<String> previousSecrets = const <String>[],
}) {
  Uint8List data;
  try {
    data = base64.decode(content);
  } on FormatException {
    throw DecryptException();
  }
  Uint8List aad = _aad(associatedData);
  Uint8List? plain = AesGcm.open(
    KeyDerivation.derive(secret, purpose),
    data,
    aad,
  );
  for (int i = 0; plain == null && i < previousSecrets.length; i++) {
    plain = AesGcm.open(
      KeyDerivation.derive(previousSecrets[i], purpose),
      data,
      aad,
    );
  }
  if (plain == null) throw DecryptException();
  try {
    return utf8.decode(plain);
  } on FormatException {
    throw DecryptException();
  }
}