rekey method

  1. @override
Future<void> rekey(
  1. List<int> newKey
)
override

Re-encrypts the database under newKey with SQLCipher's PRAGMA rekey. All data is preserved. Subsequent opens and recovery snapshots use newKey, and an existing recovery snapshot is rewritten under it at once: a passphrase is usually rotated because the old one leaked, so a copy under the old key must not wait for the next snapshot tick. Quarantined <file>.corrupt-* copies keep the key they had; delete them once inspected.

Fails closed like init: throws SqlCipherUnavailableException when the linked library is not a SQLCipher build (the rekey would be silently ignored and the file left in plaintext).

Implementation

@override
Future<void> rekey(List<int> newKey) async {
  _encryptionKey = List<int>.unmodifiable(newKey);
  final db = _db;
  if (db == null) return;
  if (db.select('PRAGMA cipher_version').isEmpty) {
    throw const SqlCipherUnavailableException();
  }
  final hex = newKey.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
  db.execute('PRAGMA rekey = "x\'$hex\'"');
  if (backend.databaseFileExists('$_path.snapshot')) snapshotNow();
}