validateEncryptionKey function

Future<bool> validateEncryptionKey({
  1. required PVCache cache,
  2. required String testKey,
  3. String testValue = '_pvcache_key_test',
})

Validates that encryption key can decrypt existing data.

Returns true if key is valid, false if key mismatch detected. Note: This function requires the cache to have throwOnFailure: false in its encryption hooks to properly detect key mismatches.

Implementation

Future<bool> validateEncryptionKey({
  required PVCache cache,
  required String testKey,
  String testValue = '_pvcache_key_test',
}) async {
  try {
    // Try to read test entry
    final result = await cache.get(testKey);

    // If test entry doesn't exist, create it
    if (result == null) {
      await cache.put(testKey, testValue, metadata: {});
      return true;
    }

    // Check if value matches
    return result == testValue;
  } catch (e) {
    // Decryption failed - key is invalid
    return false;
  }
}