deriveAndStoreKey method
Future<EncryptionOptions>
deriveAndStoreKey({
- required String password,
- String? keyName,
- EncryptionAlgorithm algorithm = EncryptionAlgorithm.aes256,
- int iterations = 10000,
Derives a key from a password and stores it in secure storage.
The password
parameter is the password to derive the key from.
The keyName
parameter can be used to specify a custom name for the key.
If not provided, a default name will be used.
Returns the generated encryption options.
Implementation
Future<EncryptionOptions> deriveAndStoreKey({
required String password,
String? keyName,
EncryptionAlgorithm algorithm = EncryptionAlgorithm.aes256,
int iterations = 10000,
}) async {
// Generate a random salt
final salt = _generateRandomString(16);
final effectiveKeyName = keyName ?? _defaultKeyName;
// Derive the key
final key = Encryption.deriveKeyFromPassword(
password: password,
salt: salt,
iterations: iterations,
);
// Store the key and salt
await _secureStorage.write(
key: _getKey(effectiveKeyName),
value: key,
);
await _secureStorage.write(
key: _getKey(_defaultSaltName),
value: salt,
);
await _secureStorage.write(
key: _getKey(_defaultAlgorithmName),
value: algorithm.toString().split('.').last,
);
_log.info('Derived and stored encryption key: $effectiveKeyName');
return EncryptionOptions(
algorithm: algorithm,
key: key,
);
}