Struct argonautica::Hasher[][src]

pub struct Hasher<'a> { /* fields omitted */ }

One of the two main structs. Use it to turn passwords into hashes

Methods

impl<'a> Hasher<'a>
[src]

Creates a new Hasher with a sensible default configuration for the average machine (e.g. an early-2014 MacBook Air).

Note: If you are using this library to hash user passwords for storage in a database, it is recommended that you adjust these settings for your machine (primarily iterations, and memory_size) until the time it takes to hash a password is approximately 300-500 milliseconds.

There is a script in the examples directory that will show you the various configuration options for your machine that produce hashing times between 300 and 500 milliseconds (Don't forget to run it with the --release and --features="simd" flags). Alternatively, you can clone the repository and run the benchmark suite with cargo bench --features="benches simd" -- inputs, which will take longer but which runs many iterations for each configuration scenario; so it provides information about distributions of running time (e.g. mean, 95% confidence intervals, etc.) as opposed to just point estimates.

Here are the default configuration options:

  • backend: Backend::C
  • cpu_pool: A CpuPool ...
    • with threads equal to the number of logical cores on your machine
    • that is lazily created, i.e. created only if / when you call the methods that need it (hash_non_blocking or hash_raw_non_blocking)
  • hash_len: 32 bytes
  • iterations: 192
  • lanes: The number of logical cores on your machine
  • memory_size: 4096 kibibytes
  • opt_out_of_secret_key: false
  • password_clearing: false
  • salt: random Salt of length 32 bytes that renews with every hash
  • secret_key_clearing: false
  • threads: The number of logical cores on your machine
  • variant: Variant::Argon2id
  • version: Version::_0x13

Creates a new Hasher that is fast but highly insecure. If for some reason you'd like to use Argon2 for hashing where security is not an issue, you can use this configuration. It sets hash length to 32 bytes (256 bits), uses only 1 iteration, sets memory size to the minimum of 8 * the number of lanes, uses a deterministic salt of the minimum length of 8 bytes, and opts out of a secret key. All other configuration options are the same as the defaults. On the developer's early-2014 Macbook Air, this configuration hashes the full text of Shakespear's Hamlet in approximately 1 millisecond (on average). MD5 does it in about half the time and sha2 with the SHA-256 algorithm performs about the same as argonautica

Allows you to configure Hasher with a custom backend. The default backend is Backend::C, which is currently the only backend supported. A Rust backend is planned, but is not currently available. If you configure a Hasher with Backend::Rust it will error when you call hash, hash_raw or their non-blocking equivalents

Allows you to configure Hasher with a custom CpuPool. The default Hasher does not have a cpu pool, which is only needed for the hash_non_blocking and hash_raw_non_blocking methods. If you call either of these methods without a cpu pool, a default cpu pool will be created for you on the fly; so even if you never configure Hasher with this method you can still use the non-blocking hashing methods. The default cpu pool has as many threads as the number of logical cores on your machine

Allows you to configure Hasher to use a custom hash length (in number of bytes). The default is 32.

See configuration example for a more detailed discussion of this parameter

Allows you to configure Hasher to use a custom number of iterations. The default is 192.

See configuration example for a more details on this parameter

Allows you to configure Hasher to use a custom number of lanes. The default is the number of physical cores on your machine.

See configuration example for a more details on this parameter

Allows you to configure Hasher to use a custom memory size (in kibibytes). The default is 4096.

See configuration example for a more details on this parameter

Allows you to configure Hasher to erase the password bytes after each call to hash, hash_raw, or their non-blocking equivalents. The default is to not clear out the password bytes (i.e. false). If you set this option to true, you must provide Hasher with a mutable password, e.g. a password constructed from a String, Vec<u8>, &mut str, &mut [u8], etc. as opposed to one constructed from a &str, &[u8], etc., or else hashing will return an Error.

See configuration example for a more details on this parameter

Allows you to configure Hasher to erase the secret key bytes after each call to hash, hash_raw, or their non-blocking equivalents. The default is to not clear out the secret key bytes (i.e. false). If you set this option to true, you must provide Hasher with a mutable secret key, e.g. a secret key constructed from a String, Vec<u8>, &mut str, &mut [u8], etc. as opposed to one constructed from a &str, &[u8], etc., or else hashing will return an Error.

See configuration example for a more details on this parameter

Allows you to configure Hasher to use a custom number of threads. The default is the number of physical cores on your machine. If you choose a number of threads that is greater than the lanes configuration, Hasher will use the minimum of the two.

See configuration example for a more details on this parameter

Allows you to configure Hasher to use a custom Argon2 variant. The default is Variant::Argon2id. Do not use a different variant unless you have a specific reason to do so.

See configuration example for a more details on this parameter

Allows you to configure Hasher to use a custom Argon2 version. The default and latest (as of 5/18) is Version::_0x13. Do not use a different version unless you have a specific reason to do so.

See configuration example for a more details on this parameter

The primary method (blocking version).

After you have configured a Hasher to your liking and provided it will all the data you would like to hash, e.g.

call this method in order to produce a string-encoded hash, which is safe to store in a database and against which you can verify passwords later

The primary method (non-blocking version).

Same as hash except it returns a Future instead of a Result

Like the hash method, but instead of producing an string-encoded hash, it produces a HashRaw struct that contains all the components of the string-encoded version, including the raw hash bytes and the raw salt bytes. In general, you should prefer to use the hash method instead of this method

Same as hash_raw except it returns a Future instead of a Result

As an extra security measure, if you want to hash without a secret key, which is not recommended, you must explicitly declare that this is your intention by calling this method and setting the opt_out_of_secret_key configuration to true (by default, it is set to false); otherwise hashing will return an error when you fail to provide a secret key

Clones the Hasher, returning a new Hasher with a static lifetime. Use this method if you would like to move a Hasher to another thread

Allows you to add some additional data to the Hasher that will be hashed alongside the Password and other pieces of data you would like to hash (i.e. the Salt and an optional SecretKey).

Including additional data in your hash is not very common; so it is unlikely you will need to use this method. If, however, you do add additional data, note that it is like a secret key in that it will be required later in order to verify passwords, and it is not stored in the string-encoded version of the hash, meaning you will have to provide it manually to a Verifier

Allows you to provide a Hasher with the password you would like to hash. Hashing requires a password; so you must call this method before calling hash, hash_raw, or their non-blocking version

Allows you to provide Hasher with a custom Salt to include in the hash. The default Hasher is configured to use a random Salt of 32 bytes; so there is no need to call this method. If you would like to use a random Salt of different length, you can call this method with Salt::random(your_custom_length_in_bytes). Using a deterministic Salt is possible, but discouraged

Allows you to provide Hasher with a secret key that will be used to create the hash. The secret key will not be included in the hash output, meaning you must save it somewhere (ideally outside your code) to use later, as the only way to verify passwords against the hash later is to know the secret key. This library encourages the use of a secret key

Read-only access to the Hasher's AdditionalData, if any

Read-only access to the Hasher's HasherConfig

Read-only access to the Hasher's Password, if any

Read-only access to the Hasher's Salt

Read-only access to the Hasher's SecretKey, if any

Trait Implementations

impl<'a> Default for Hasher<'a>
[src]

Same as the new method

impl<'a> Debug for Hasher<'a>
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<'a> Send for Hasher<'a>

impl<'a> Sync for Hasher<'a>