libsodium_crypto_pwhash_scryptsalsa208sha256 (libsodium v3.0.1)

View Source

Password hashing and password-based key derivation with scrypt (scryptsalsa208sha256). Prefer Argon2id through libsodium_crypto_pwhash for new applications; use scrypt for interoperability with existing scrypt hashes or keys.

str/3 produces a self-describing $7$ string to store and check with str_verify/2; crypto_pwhash_scryptsalsa208sha256/5 derives a key from a password and a saltbytes/0 byte salt, and ll/6 exposes the raw scrypt function with explicit N, R, and P parameters. Raise Opslimit and Memlimit (memory in bytes) as far as your latency budget allows, starting from the _interactive limits for online logins; libsodium raises values below the minimums to the minimums. All hashing runs on a dirty CPU scheduler.

Ops = libsodium_crypto_pwhash_scryptsalsa208sha256:opslimit_interactive(),
Mem = libsodium_crypto_pwhash_scryptsalsa208sha256:memlimit_interactive(),
Str = libsodium_crypto_pwhash_scryptsalsa208sha256:str(<<"correct horse">>, Ops, Mem),
0 = libsodium_crypto_pwhash_scryptsalsa208sha256:str_verify(Str, <<"correct horse">>),
-1 = libsodium_crypto_pwhash_scryptsalsa208sha256:str_verify(Str, <<"wrong horse">>),
0 = libsodium_crypto_pwhash_scryptsalsa208sha256:str_needs_rehash(Str, Ops, Mem).

Summary

Types

A crypto_pwhash_scryptsalsa208sha256_SALTBYTES (32-byte) binary.

Functions

Return crypto_pwhash_scryptsalsa208sha256_bytes_max(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_bytes_min(), in bytes.

Derive an Outlen byte key from Passwd and the saltbytes/0 byte Salt with the cost parameters Opslimit and Memlimit.

Compute raw scrypt over Passwd and Salt with the CPU/memory cost N (a power of two greater than one), block size R, and parallelism P, producing Buflen bytes.

Return crypto_pwhash_scryptsalsa208sha256_memlimit_interactive(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_memlimit_max(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_memlimit_min(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_max(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_min(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_passwd_max(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_passwd_min(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_saltbytes(), in bytes.

Hash Passwd for storage with the cost parameters Opslimit and Memlimit and a random salt.

Check whether Str was hashed with exactly the parameters that Opslimit and Memlimit select.

Check Passwd against Str, a $7$ string produced by str/3.

Return crypto_pwhash_scryptsalsa208sha256_strbytes(), in bytes.

Return crypto_pwhash_scryptsalsa208sha256_strprefix() as a binary.

Types

salt()

-type salt() :: <<_:256>>.

A crypto_pwhash_scryptsalsa208sha256_SALTBYTES (32-byte) binary.

Functions

bytes_max()

-spec bytes_max() -> Result when Result :: non_neg_integer().

Return crypto_pwhash_scryptsalsa208sha256_bytes_max(), in bytes.

bytes_min()

-spec bytes_min() -> Result when Result :: 16.

Return crypto_pwhash_scryptsalsa208sha256_bytes_min(), in bytes.

crypto_pwhash_scryptsalsa208sha256(Outlen, Passwd, Salt, Opslimit, Memlimit)

-spec crypto_pwhash_scryptsalsa208sha256(Outlen, Passwd, Salt, Opslimit, Memlimit) -> Result
                                            when
                                                Outlen :: non_neg_integer(),
                                                Passwd :: binary(),
                                                Salt :: salt(),
                                                Opslimit :: non_neg_integer(),
                                                Memlimit :: non_neg_integer(),
                                                Result :: binary() | -1.

Derive an Outlen byte key from Passwd and the saltbytes/0 byte Salt with the cost parameters Opslimit and Memlimit.

Returns -1 when libsodium rejects the parameters (for example an Outlen below bytes_min/0) or cannot allocate the requested memory.

ll(Passwd, Salt, N, R, P, Buflen)

-spec ll(Passwd, Salt, N, R, P, Buflen) -> Result
            when
                Passwd :: binary(),
                Salt :: binary(),
                N :: non_neg_integer(),
                R :: 0..4294967295,
                P :: 0..4294967295,
                Buflen :: non_neg_integer(),
                Result :: binary() | -1.

Compute raw scrypt over Passwd and Salt with the CPU/memory cost N (a power of two greater than one), block size R, and parallelism P, producing Buflen bytes.

Returns -1 when the parameters are invalid or the required memory cannot be allocated.

memlimit_interactive()

-spec memlimit_interactive() -> Result when Result :: 16777216.

Return crypto_pwhash_scryptsalsa208sha256_memlimit_interactive(), in bytes.

memlimit_max()

-spec memlimit_max() -> Result when Result :: non_neg_integer().

Return crypto_pwhash_scryptsalsa208sha256_memlimit_max(), in bytes.

memlimit_min()

-spec memlimit_min() -> Result when Result :: 16777216.

Return crypto_pwhash_scryptsalsa208sha256_memlimit_min(), in bytes.

memlimit_sensitive()

-spec memlimit_sensitive() -> Result when Result :: 1073741824.

Return crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive(), in bytes.

opslimit_interactive()

-spec opslimit_interactive() -> Result when Result :: 524288.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(), in bytes.

opslimit_max()

-spec opslimit_max() -> Result when Result :: 4294967295.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_max(), in bytes.

opslimit_min()

-spec opslimit_min() -> Result when Result :: 32768.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_min(), in bytes.

opslimit_sensitive()

-spec opslimit_sensitive() -> Result when Result :: 33554432.

Return crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive(), in bytes.

passwd_max()

-spec passwd_max() -> Result when Result :: non_neg_integer().

Return crypto_pwhash_scryptsalsa208sha256_passwd_max(), in bytes.

passwd_min()

-spec passwd_min() -> Result when Result :: 0.

Return crypto_pwhash_scryptsalsa208sha256_passwd_min(), in bytes.

saltbytes()

-spec saltbytes() -> Result when Result :: 32.

Return crypto_pwhash_scryptsalsa208sha256_saltbytes(), in bytes.

str(Passwd, Opslimit, Memlimit)

-spec str(Passwd, Opslimit, Memlimit) -> Result
             when
                 Passwd :: binary(),
                 Opslimit :: non_neg_integer(),
                 Memlimit :: non_neg_integer(),
                 Result :: binary() | -1.

Hash Passwd for storage with the cost parameters Opslimit and Memlimit and a random salt.

Returns an ASCII binary of strbytes/0 - 1 bytes starting with strprefix/0, or -1 on failure.

str_needs_rehash(Str, Opslimit, Memlimit)

-spec str_needs_rehash(Str, Opslimit, Memlimit) -> Result
                          when
                              Str :: binary(),
                              Opslimit :: non_neg_integer(),
                              Memlimit :: non_neg_integer(),
                              Result :: -1 | 0 | 1.

Check whether Str was hashed with exactly the parameters that Opslimit and Memlimit select.

Returns 0 when it was, 1 when the password should be rehashed (after a successful str_verify/2), and -1 when Str is malformed.

str_verify(Str, Passwd)

-spec str_verify(Str, Passwd) -> Result when Str :: binary(), Passwd :: binary(), Result :: 0 | -1.

Check Passwd against Str, a $7$ string produced by str/3.

Returns 0 when the password matches and -1 when it does not or Str is malformed.

strbytes()

-spec strbytes() -> Result when Result :: 102.

Return crypto_pwhash_scryptsalsa208sha256_strbytes(), in bytes.

strprefix()

-spec strprefix() -> Result when Result :: binary().

Return crypto_pwhash_scryptsalsa208sha256_strprefix() as a binary.