rotateEncryptionKey function

Future<void> rotateEncryptionKey({
  1. required PVCache cache,
  2. String keyName = '_pvcache_encryption_key',
  3. String? newKey,
})

Rotates encryption key and clears all encrypted data.

Use this when you need to change the encryption key. WARNING: This will clear all encrypted cache entries.

Implementation

Future<void> rotateEncryptionKey({
  required PVCache cache,
  String keyName = '_pvcache_encryption_key',
  String? newKey,
}) async {
  // Clear all cache data (encrypted data becomes unreadable)
  await cache.clear();

  // Skip secure storage operations in test mode
  if (!PVBridge.testMode) {
    // Delete old key from secure storage
    await PVBridge.secureStorage.delete(key: keyName);

    // Store new key if provided, otherwise will be auto-generated on next use
    if (newKey != null) {
      await PVBridge.secureStorage.write(key: keyName, value: newKey);
    }
  }
}