pwHash static method

Future<List<int>> pwHash(
  1. String input,
  2. String salt,
  3. int iterations,
  4. int bitLengthKey,
)

This method takes a string input, a salt string salt, an integer iterations for the number of iterations of the PBKDF2 algorithm, and an integer bitLengthKey for the bit length of the derived key. It then uses the PBKDF2 algorithm with HMAC-SHA512 as the MAC algorithm to derive a key from the input and salt strings. The derived key is returned as a list of integers.

Implementation

static Future<List<int>> pwHash(
    String input, String salt, int iterations, int bitLengthKey) async {
  final pbkdf2 = Pbkdf2(
      macAlgorithm: Hmac.sha512(),
      iterations: iterations,
      bits: bitLengthKey);
  final secretKey = SecretKey(utf8.encode(input));

  // A random salt
  final nonce = utf8.encode(salt);

  // Calculate a hash that can be stored in the database
  final newSecretKey = await pbkdf2.deriveKey(
    secretKey: secretKey,
    nonce: nonce,
  );
  final newSecretKeyBytes = await newSecretKey.extractBytes();
  return newSecretKeyBytes;
}