getOrCreateEncryptionKey function
Get or generate encryption key from secure storage.
Keys stored in flutter_secure_storage.
Implementation
Future<String> getOrCreateEncryptionKey(String keyName) async {
// Try to read existing key
final existingKey = await PVBridge.secureStorage.read(key: keyName);
if (existingKey != null && existingKey.isNotEmpty) {
return existingKey;
}
// Generate new key (32 characters for AES-256 compatibility)
final timestamp = DateTime.now().millisecondsSinceEpoch;
final random = timestamp.toString() + DateTime.now().microsecond.toString();
final hash = sha256.convert(utf8.encode(random)).toString();
final newKey = hash.substring(0, 32);
// Store in secure storage
await PVBridge.secureStorage.write(key: keyName, value: newKey);
return newKey;
}