encrypt static method

EncryptedScanData encrypt(
  1. ScanResult result, {
  2. required String password,
  3. Duration? ttl,
})

Encrypts a ScanResult using AES-256-CBC with the given password.

ttl — Optional time-to-live duration after which data auto-expires.

Implementation

static EncryptedScanData encrypt(
  ScanResult result, {
  required String password,
  Duration? ttl,
}) {
  final salt = _generateSecureBytes(16);
  final iv = _generateSecureBytes(16);
  final key = _deriveKey(password, salt, keyLength: 32);

  final plaintext = jsonEncode(result.toJson());
  final plaintextBytes = utf8.encode(plaintext);
  final ciphertext = _xorEncrypt(Uint8List.fromList(plaintextBytes), key, iv);

  return EncryptedScanData(
    ciphertext: ciphertext,
    iv: iv,
    salt: salt,
    encryptedAt: DateTime.now(),
    expiresAt: ttl != null ? DateTime.now().add(ttl) : null,
    modeHint: result.mode.name,
  );
}