hashPasswordBytesSync method

DArgon2Result hashPasswordBytesSync(
  1. List<int> password, {
  2. required Salt salt,
  3. int iterations = 32,
  4. int memory = 256,
  5. int parallelism = 2,
  6. int length = 32,
  7. Argon2Type type = Argon2Type.i,
  8. Argon2Version version = Argon2Version.V13,
})

The method to hash a byte array of type List

Needs a List of type int password and a salt to be given with an optional parameters to control the amount of iterations, memory, parallelism used during the operation. Also optionally takes a length parameter for the hash's return length, as well as a type and version to pass along to the C method.

Returns a DArgon2Result with the hashed password, encoded hash, and various conversion options for the hash and encoded bytes.

Implementation

DArgon2Result hashPasswordBytesSync(List<int> password,
    {required Salt salt,
    int iterations = 32,
    int memory = 256,
    int parallelism = 2,
    int length = 32,
    Argon2Type type = Argon2Type.i,
    Argon2Version version = Argon2Version.V13}) {
  //Create pointers to pass to the C method
  var passPointer = _setPtr(password);
  var saltPointer = _setPtr(salt.bytes);
  var hash = _setPtr(List.filled(length, 0));
  //Get the length of the encoded hash and set the encoded pointer
  var encodedLength = LocalBinder.instance.getEncodedHashLength(
      iterations, memory, parallelism, salt.bytes.length, length, type.index);
  var encoded = _setPtr(List.filled(encodedLength, 0));
  var v = version == Argon2Version.V13 ? 0x13 : 0x10;
  var result = LocalBinder.instance.getHash(
      iterations,
      memory,
      parallelism,
      passPointer,
      password.length,
      saltPointer,
      salt.bytes.length,
      hash,
      length,
      encoded,
      encodedLength,
      type.index,
      v);
  //Create new Lists of the hash and encoded values
  var hashBytes = List<int>.from(hash.asTypedList(length).cast());
  var encodedBytes =
      List<int>.from(encoded.asTypedList(encodedLength).cast());
  if (encodedBytes.last == 0) {
    encodedBytes.removeLast();
  }
  //Free all pointers
  malloc.free(hash);
  malloc.free(encoded);
  malloc.free(saltPointer);
  malloc.free(passPointer);
  if (DArgon2ErrorCode.values[result.abs()] != DArgon2ErrorCode.ARGON2_OK) {
    throw DArgon2Exception(
        LocalBinder.instance.getErrorMessage(result).toDartString(),
        DArgon2ErrorCode.values[result.abs()]);
  }
  return DArgon2Result(hashBytes, encodedBytes);
}