View Source Argon2.Base (argon2_elixir v3.1.0)

Lower-level api for Argon2.

These functions can be useful if you want more control over some of the options. In most cases, you will not need to call these functions directly.

Link to this section Summary

Link to this section Functions

Link to this function

encodedlen_nif(t_cost, m_cost, parallelism, saltlen, hashlen, argon2_type)

View Source

Calculate the length of the encoded hash.

Translate the error code to an error message.

Link to this function

gen_salt(salt_len \\ 16)

View Source

Generate a random salt.

The default length for the salt is 16 bytes. We do not recommend using a salt shorter than the default.

Link to this function

hash_nif(t_cost, m_cost, parallelism, password, salt, raw, hashlen, encodedlen, argon2_type, argon2_version)

View Source

Hash a password using Argon2.

Link to this function

hash_password(password, salt, opts \\ [])

View Source

Hash a password using Argon2.

options

Options

There are six options:

  • :t_cost - time cost
    • the default is 8
  • :m_cost - memory usage
    • the default is 16 - this will produce a memory usage of 64 MiB (2 ^ 16 KiB)
  • :parallelism - number of parallel threads
    • the default is 2
  • :format - output format
    • this value can be
      • :encoded - encoded with Argon2 crypt format
      • :raw_hash - raw hash output in hexadecimal format
      • :report - raw hash and encoded hash, together with the options used
    • :encoded is the default
  • :hashlen - length of the hash (in bytes)
    • the default is 32
  • :argon2_type - Argon2 type
    • this value should be 0 (Argon2d), 1 (Argon2i) or 2 (Argon2id)
    • the default is 2 (Argon2id)

The t_cost, m_cost, parallelism and argon2_type can also be set in the config. See the documentation for Argon2.Stats for more information about choosing these values.

examples

Examples

The following example changes the default t_cost and m_cost:

Argon2.Base.hash_password("password", "somesaltSOMESALT", [t_cost: 4, m_cost: 18])

In the example below, the Argon2 type is changed to Argon2d:

Argon2.Base.hash_password("password", "somesaltSOMESALT", [argon2_type: 0])

To use Argon2i, use argon2_type: 1.

Link to this function

verify_nif(stored_hash, password, argon2_type)

View Source

Verify a password using Argon2.