createEncryptedHook function

Future<HHPlugin> createEncryptedHook({
  1. KeyRotationStrategy rotationStrategy = KeyRotationStrategy.passive,
  2. String secureStorageKey = 'pvcache_encryption_key',
  3. Uint8List? providedKey,
  4. bool autoResetKey = false,
  5. EncryptionHookController? controller,
  6. Future<bool> rotationCallback(
    1. Object error,
    2. String key
    )?,
})

Creates an encryption plugin using AES-256-CBC.

Example:

final plugin = await createEncryptedHook(
  rotationStrategy: KeyRotationStrategy.passive,
);
PVCache.setDefaultPlugins([plugin]);

Implementation

Future<HHPlugin> createEncryptedHook({
  KeyRotationStrategy rotationStrategy = KeyRotationStrategy.passive,
  String secureStorageKey = 'pvcache_encryption_key',
  Uint8List? providedKey,
  bool autoResetKey = false,
  EncryptionHookController? controller,
  Future<bool> Function(Object error, String key)? rotationCallback,
}) async {
  final keyManager = EncryptionKeyManager(
    storageKey: secureStorageKey,
    providedKey: providedKey,
    autoResetKey: autoResetKey,
  );

  await keyManager.initialize();

  final hook = _EncryptionTerminalHook(
    keyManager: keyManager,
    rotationStrategy: rotationStrategy,
    controller: controller,
    rotationCallback: rotationCallback,
    id: 'pvcache_encryption_${_encryptionHookIdCounter++}',
  );

  return HHPlugin(terminalSerializationHooks: [hook]);
}