deriveKey function

Uint8List deriveKey(
  1. String password,
  2. String salt, {
  3. int length = 32,
  4. int iterations = 100000,
})

Derives an AES key from a password and salt using the PBKDF2 algorithm.

This function uses PBKDF2 with HMAC-SHA256 to generate a key of length (default is 32 bytes) from the user's password. The number of iterations (default is 100000) is used to strengthen the security.

Implementation

Uint8List deriveKey(
  String password,
  String salt, {
  int length = 32,
  int iterations = 100000,
}) {
  final derivator = PBKDF2KeyDerivator(HMac(SHA256Digest(), 64));
  derivator.init(Pbkdf2Parameters(utf8.encode(salt), iterations, length));
  return derivator.process(utf8.encode(password));
}