deriveKey static method

Uint8List deriveKey(
  1. String password,
  2. Uint8List salt, {
  3. int iterations = defaultIterations,
  4. int keyLength = keySize,
})

Derives a key from a password using PBKDF2-HMAC-SHA256 (RFC 2898).

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 passwordBytes = Uint8List.fromList(utf8.encode(password));
  final key = _pbkdf2HmacSha256(passwordBytes, salt, iterations, keyLength);

  // Zero password bytes
  zeroMemory(passwordBytes);

  return key;
}