decrypt static method

ScanResult? decrypt(
  1. EncryptedScanData encrypted, {
  2. required String password,
  3. bool ignoreExpiry = false,
})

Decrypts an EncryptedScanData envelope back to a ScanResult.

Throws EncryptionException if the password is incorrect or data is corrupted. Returns null if the data has expired.

Implementation

static ScanResult? decrypt(
  EncryptedScanData encrypted, {
  required String password,
  bool ignoreExpiry = false,
}) {
  if (!ignoreExpiry && encrypted.isExpired) {
    return null;
  }

  final key = _deriveKey(password, encrypted.salt, keyLength: 32);
  final decryptedBytes =
      _xorDecrypt(encrypted.ciphertext, key, encrypted.iv);

  try {
    final jsonStr = utf8.decode(decryptedBytes);
    final jsonMap = jsonDecode(jsonStr) as Map<String, dynamic>;
    return _scanResultFromJson(jsonMap);
  } catch (_) {
    throw EncryptionException(
        'Decryption failed — incorrect password or corrupted data');
  }
}