HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulelibsodium-bindings-0.0.1.1Haskell2010

LibSodium.Bindings.PasswordHashing

  • 24 values

Introduction

0 declarations

This modules provides an API that can be used both for key derivation using a low-entropy input and password storage.

Guidelines for choosing the parameters

Start by determining how much memory the function can use. What will be the highest number of threads/processes evaluating the function simultaneously (ideally, no more than 1 per CPU core)? How much physical memory is guaranteed to be available?

Set memlimit to the amount of memory you want to reserve for password hashing.

Then set opslimit to 3 and measure the time it takes to hash a password.

If this is way too long for your application, reduce memlimit, but keep opslimit set to 3.

If the function is so fast that you can afford it to be more computationally intensive without any usability issues, then increase opslimit.

For online use (e.g. logging in on a website), a 1 second computation is likely to be the acceptable maximum.

For interactive use (e.g. a desktop application), a 5 second pause after having entered a password is acceptable if the password doesn't need to be entered more than once per session.

For non-interactive and infrequent use (e.g. restoring an encrypted backup), an even slower computation can be an option.

However, the best defense against brute-force password cracking is to use strong passwords. Libraries such as passwdqc can help enforce this.

Operations

4 declarations
valuecryptoPWHash
  1. :: Ptr CUChar

    out parameter. It represents the address of a dedicated storage area of outlen bytes.

  2. -> CLLong

    outlen parameter. It is the length of the key derived from the passwd parameter. This should be a least cryptoPWHashBytesMin and at most cryptoPWHashBytesMax.

  3. -> Ptr CChar

    passwd parameter. It is a pointer to the password that is to be derived.

  4. -> CULLong

    passwdlen parameter. It is the size of the password.

  5. -> Ptr CUChar

    salt parameter. It is of a fixed length established by cryptoPWHashSaltBytes. It should be unpredictable. randombytesBuf is the best way to fill the cryptoPWHashSaltBytes of the salt.

  6. -> CULLong

    opslimit parameter. It represents the maximum amount of computations to perform. Raising this number will make the function require more CPU cycles to compute a key. This number must be between cryptoPWHashOpsLimitMin and cryptoPWHashOpsLimitMax.

  7. -> CSize

    memlimit parameter. It is the maximum amount of RAM in bytes that the function will use. This number must be between cryptoPWHashMemLimitMin cryptoPWHashMemLimitMax

  8. -> CInt

    alg parameter. It is an identifier for the algorithm to use and should be set to one of the following values: cryptoPWHashAlgDefault, cryptoPWHashAlgArgon2I13 or cryptoPWHashAlgArgon2ID13.

  9. -> IO CInt

    The return code is 0 on success and -1 if the computation didn't complete, usually because the operating system refused to allocate the amount of requested memory.

#

This functions derives a key from a password. The computed key is then stored in the out parameter.

See: crypto_pwhash()

valuecryptoPWHashStr
  1. :: Ptr CChar

    out parameter. It must be of size cryptoPWHashStrBytes.

  2. -> Ptr CChar

    passwd parameter. Points to a password to be stored.

  3. -> CULLong

    passwdlen parameter. Length of the password.

  4. -> CULLong

    opslimit parameter. It represents the maximum amount of computations to perform.

  5. -> CSize

    memlimit parameter. It is the maximum amount of RAM in bytes that the function will use.

  6. -> IO CInt

    The function returns 0 on success and -1 if it didn't complete successfully.

#

This function is used for password storage, like an SQL database. It stores an ASCII-encoded string into its out parameter, which includes:

  • The result of a memory-hard, CPU-intensive hash function applied to the password passwd of length passwdlen;

  • The automatically generated salt used for the previous computation;

  • The other parameters required to verify the password, including the algorithm identifier, its version, opslimit, and memlimit.

The out parameter must be a dedicated storage area that's large enough to hold cryptoPWHashStrBytes bytes, but the actual output string may be shorter.

See: crypto_pwhash_str()

valuecryptoPWHashStrNeedsRehash
  1. :: Ptr CChar

    str parameter.

  2. -> CULLong

    opslimit parameter.

  3. -> CSize

    memlimit parameter.

  4. -> IO CInt

    The function returns 0 if the parameters already match the given ones, and returns 1 on error. In particular, It will return 1 if the string appears to be correct but doesn't match the given parameters. In that situation, applications may want to compute a new hash using the current parameters the next time the user logs in.

#

This functions checks if a password verification string str matches the parameters opslimit, memlimit, and the current default algorithm.

See: crypto_pwhash_str_needs_rehash()

Constants

20 declarations