deriveKeyFromPassword static method

String deriveKeyFromPassword({
  1. required String password,
  2. required String salt,
  3. int iterations = 10000,
  4. int keyLength = 32,
})

Derives a key from a password using PBKDF2.

The password parameter is the password to derive the key from. The salt parameter is a random value used to prevent dictionary attacks. The iterations parameter is the number of iterations to perform. The keyLength parameter is the length of the derived key in bytes.

Returns the derived key as a base64-encoded string.

Implementation

static String deriveKeyFromPassword({
  required String password,
  required String salt,
  int iterations = 10000,
  int keyLength = 32,
}) {
  final bytes = utf8.encode(password);
  final saltBytes = utf8.encode(salt);

  // Use PBKDF2 to derive a key from the password
  final key = pbkdf2(
    bytes: bytes,
    salt: saltBytes,
    iterations: iterations,
    keyLength: keyLength,
    hashAlgorithm: sha256,
  );

  return base64Encode(key);
}