Struct password_hash::Salt[][src]

pub struct Salt<'a>(_);
Expand description

Salt string.

In password hashing, a “salt” is an additional value used to personalize/tweak the output of a password hashing function for a given input password.

Salts help defend against attacks based on precomputed tables of hashed passwords, i.e. “rainbow tables”.

The Salt type implements the RECOMMENDED best practices for salts described in the PHC string format specification, namely:

  • Maximum lengths for salt, output and parameter values are meant to help consumer implementations, in particular written in C and using stack-allocated buffers. These buffers must account for the worst case, i.e. the maximum defined length. Therefore, keep these lengths low.
  • The role of salts is to achieve uniqueness. A random salt is fine for that as long as its length is sufficient; a 16-byte salt would work well (by definition, UUID are very good salts, and they encode over exactly 16 bytes). 16 bytes encode as 22 characters in B64. Functions should disallow salt values that are too small for security (4 bytes should be viewed as an absolute minimum).

Recommended length

The recommended default length for a salt string is 16-bytes (128-bits).

See below for rationale.

Constraints

Salt strings are constrained to the following set of characters per the PHC spec:

The salt consists in a sequence of characters in: [a-zA-Z0-9/+.-] (lowercase letters, uppercase letters, digits, /, +, . and -).

Additionally the following length restrictions are enforced based on the guidelines from the spec:

  • Minimum length: 4-bytes
  • Maximum length: 64-bytes

A maximum length is enforced based on the above recommendation for supporting stack-allocated buffers (which this library uses), and the specific determination of 64-bytes is taken as a best practice from the Argon2 Encoding specification in the same document:

The length in bytes of the salt is between 8 and 64 bytes, thus yielding a length in characters between 11 and 64 characters (and that length is never equal to 1 modulo 4). The default byte length of the salt is 16 bytes (22 characters in B64 encoding). An encoded UUID, or a sequence of 16 bytes produced with a cryptographically strong PRNG, are appropriate salt values.

The Argon2 specification states that the salt can be much longer, up to 2^32-1 bytes, but this makes little sense for password hashing. Specifying a relatively small maximum length allows for parsing with a stack allocated buffer.)

Based on this guidance, this type enforces an upper bound of 64-bytes as a reasonable maximum, and recommends using 16-bytes.

Implementations

Minimum length of a Salt string: 4-bytes.

Maximum length of a Salt string: 64-bytes.

See type-level documentation about Salt for more information.

Recommended length of a salt: 16-bytes.

Create a Salt from the given str, validating it according to Salt::MIN_LENGTH and Salt::MAX_LENGTH length restrictions.

Attempt to decode a B64-encoded Salt, writing the decoded result into the provided buffer, and returning a slice of the buffer containing the decoded result on success.

Borrow this value as a str.

Borrow this value as bytes.

Get the length of this value in ASCII characters.

Trait Implementations

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.