deriveKey static method
Derives a key from a password using PBKDF2-HMAC-SHA256.
password - The password to derive from (must not be empty)
salt - Random salt (must be at least 8 bytes, use generateSalt)
iterations - PBKDF2 iterations (default defaultIterations)
keyLength - Output key length in bytes (default keySize)
Returns the derived key as a Uint8List.
Implementation
static Uint8List deriveKey(
String password,
Uint8List salt, {
int iterations = defaultIterations,
int keyLength = keySize,
}) {
if (password.isEmpty) {
throw ArgumentError('Password cannot be empty');
}
if (salt.length < 8) {
throw ArgumentError('Salt must be at least 8 bytes');
}
final pbkdf2 = PBKDF2KeyDerivator(HMac(SHA256Digest(), 64));
pbkdf2.init(Pbkdf2Parameters(salt, iterations, keyLength));
final passwordBytes = Uint8List.fromList(utf8.encode(password));
final key = Uint8List(keyLength);
pbkdf2.deriveKey(passwordBytes, 0, key, 0);
// Zero password bytes
zeroMemory(passwordBytes);
return key;
}