hashPassword method

Map<String, String> hashPassword(
  1. String password, {
  2. String? salt,
  3. int iterations = _defaultIterations,
  4. int keyLength = _defaultKeyLength,
})

Hash a password using PBKDF2 with salt

password is the plain text password to hash salt is an optional custom salt (will generate one if not provided) iterations is the number of iterations for key derivation keyLength is the length of the derived key in bytes

Returns a map containing the hashed password and salt

Implementation

Map<String, String> hashPassword(
  String password, {
  String? salt,
  int iterations = _defaultIterations,
  int keyLength = _defaultKeyLength,
}) {
  final saltBytes = salt != null
      ? base64.decode(salt)
      : _generateRandomBytes(_defaultSaltLength);

  final key = _pbkdf2(
    password.codeUnits,
    saltBytes,
    iterations,
    keyLength,
  );

  return {
    'hash': base64.encode(key),
    'salt': base64.encode(saltBytes),
    'iterations': iterations.toString(),
    'keyLength': keyLength.toString(),
  };
}