HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulepassword-3.0.4.0Haskell2010

Data.Password.Argon2

Argon2

Argon2 is probably the newest password algorithm out there. Argon2 was selected as the winner of the Password Hashing Competition in July 2015.

It has three variants, namely Argon2d, Argon2i and Argon2id. These protect against GPU cracking attacks, side-channel attacks, and both, respectively.

All three modes allow specification by three parameters that control:

  • execution time

  • memory required

  • degree of parallelism

Other algorithms

In comparison to other algorithms, Argon2 is the least "battle-tested", being the newest algorithm out there.

It is, however, recommended over Scrypt most of the time, and it also seems like it might become the go-to password algorithm if no vulnarabilities are discovered within the next couple of years.

  • 8 types
  • 9 values
  • Packagepassword-3.0.4.0
  • Exports17
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceArgon2.hs
datadata Argon2
#

Phantom type for Argon2

Plain-text Password

2 declarations
newtypenewtype Password
#

A plain-text password.

This represents a plain-text password that has NOT been hashed.

You should be careful with Password. Make sure not to write it to logs or store it in a database.

You can construct a Password by using the mkPassword function or as literal strings together with the OverloadedStrings pragma (or manually, by using fromString on a String). Alternatively, you could also use some of the instances in the password-instances library.

Instances2Show, IsString
  • Show PasswordDefined in password-types-1.0.0.0 · Data.Password.Types

    CAREFUL: Show-ing a Password will always print "**PASSWORD**"

    Example1 expression
    show ("hello" :: Password)"**PASSWORD**"
  • IsString PasswordDefined in password-types-1.0.0.0 · Data.Password.Types

Hash Passwords (Argon2)

2 declarations
newtypenewtype PasswordHash a
#

A hashed password.

This represents a password that has been put through a hashing function. The hashed password can be stored in a database.

Instances4Eq, Ord, Read, Show
  • Eq (PasswordHash a)Defined in password-types-1.0.0.0 · Data.Password.Types
  • Ord (PasswordHash a)Defined in password-types-1.0.0.0 · Data.Password.Types
  • Read (PasswordHash a)Defined in password-types-1.0.0.0 · Data.Password.Types
  • Show (PasswordHash a)Defined in password-types-1.0.0.0 · Data.Password.Types

Verify Passwords (Argon2)

2 declarations

Check a Password against a PasswordHash Argon2.

Returns PasswordCheckSuccess on success.

Example3 expressions
let pass = mkPassword "foobar"passHash <- hashPassword passcheckPassword pass passHashPasswordCheckSuccess

Returns PasswordCheckFail if an incorrect Password or PasswordHash Argon2 is used.

Example2 expressions
let badpass = mkPassword "incorrect-password"checkPassword badpass passHashPasswordCheckFail

This should always fail if an incorrect password is given.

Property
\(Blind badpass) -> let correctPasswordHash = hashPasswordWithSalt testParams salt "foobar" in checkPassword badpass correctPasswordHash == PasswordCheckFail
datadata PasswordCheck
#

The result of checking a password against a hashed version. This is returned by the checkPassword functions.

Constructors

  • PasswordCheckSuccess

    The password check was successful. The plain-text password matches the hashed password.

  • PasswordCheckFail

    The password check failed. The plain-text password does not match the hashed password.

Instances3Eq, Read, Show

Hashing Manually (Argon2)

6 declarations

Hash a password using the Argon2 algorithm with the given Argon2Params.

N.B.: If you have any doubt in your knowledge of cryptography and/or the Argon2 algorithm, please just use hashPassword.

Advice to set the parameters:

  • Figure out how many threads you can use, choose "parallelism" accordingly.

  • Figure out how much memory you can use, choose "memory cost" accordingly.

  • Decide on the maximum time x you can spend on it, choose the largest "time cost" such that it takes less than x with your system and other parameter choices.

Default parameters for the Argon2 algorithm.

Example1 expression
defaultParamsArgon2Params {argon2Salt = 16, argon2Variant = Argon2id, argon2Version = Version13, argon2MemoryCost = 65536, argon2TimeCost = 2, argon2Parallelism = 1, argon2OutputLength = 32}
datadata Argon2Params
#

Parameters used in the Argon2 hashing algorithm.

Constructors

Instances2Eq, Show
datadata Variant
#

Which variant of Argon2 to use. You should choose the variant that is most applicable to your intention to hash inputs.

Constructors

  • Argon2d

    Argon2d is faster than Argon2i and uses data-depending memory access, which makes it suitable for cryptocurrencies and applications with no threats from side-channel timing attacks.

  • Argon2i

    Argon2i uses data-independent memory access, which is preferred for password hashing and password-based key derivation. Argon2i is slower as it makes more passes over the memory to protect from tradeoff attacks.

  • Argon2id

    Argon2id is a hybrid of Argon2i and Argon2d, using a combination of data-depending and data-independent memory accesses, which gives some of Argon2i's resistance to side-channel cache timing attacks and much of Argon2d's resistance to GPU cracking attacks

Instances6Bounded, Enum, Eq, Ord, Read, Show
  • Bounded VariantDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Enum VariantDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Eq VariantDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Ord VariantDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Read VariantDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Show VariantDefined in cryptonite-0.30 · Crypto.KDF.Argon2
datadata Version
#

Which version of Argon2 to use

Instances6Bounded, Enum, Eq, Ord, Read, Show
  • Bounded VersionDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Enum VersionDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Eq VersionDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Ord VersionDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Read VersionDefined in cryptonite-0.30 · Crypto.KDF.Argon2
  • Show VersionDefined in cryptonite-0.30 · Crypto.KDF.Argon2

Hashing with salt (DISADVISED)

Hashing with a set Salt is almost never what you want to do. Use hashPassword or hashPasswordWithParams to have automatic generation of randomized salts.

Hash a password with the given Argon2Params and also with the given Salt instead of a random generated salt using argon2Salt from Argon2Params. (cf. hashPasswordWithParams) Using hashPasswordWithSalt is strongly disadvised and hashPasswordWithParams should be used instead. Never use a static salt in production applications!

N.B.: The salt HAS to be 8 bytes or more, or this function will throw an error!

Example2 expressions
let salt = Salt "abcdefghijklmnop"hashPasswordWithSalt defaultParams salt (mkPassword "foobar")PasswordHash {unPasswordHash = "$argon2id$v=19$m=65536,t=2,p=1$YWJjZGVmZ2hpamtsbW5vcA$BztdyfEefG5V18ZNlztPrfZaU5duVFKZiI6dJeWht0o"}

(Note that we use an explicit Salt in the example above. This is so that the example is reproducible, but in general you should use hashPassword. hashPassword generates a new Salt everytime it is called.)

newtypenewtype Salt a
#

A salt used by a hashing algorithm.

Constructors

Instances2Eq, Show
  • Eq (Salt a)Defined in password-types-1.0.0.0 · Data.Password.Types
  • Show (Salt a)Defined in password-types-1.0.0.0 · Data.Password.Types

Unsafe debugging function to show a Password

1 declaration

This is an unsafe function that shows a password in plain-text.

Example1 expression
unsafeShowPassword ("foobar" :: Password)"foobar"

You should generally not use this function in production settings, as you don't want to accidentally print a password anywhere, like logs, network responses, database entries, etc.

This will mostly be used by other libraries to handle the actual password internally, though it is conceivable that, even in a production setting, a password might have to be handled in an unsafe manner at some point.

Setup for doctests.

0 declarations
Example2 expressions
:set -XFlexibleInstances:set -XOverloadedStrings

Import needed libraries.

Example4 expressions
import Data.Password.Typesimport Data.ByteString (pack)import Test.QuickCheck (Arbitrary(arbitrary), Blind(Blind), vector)import Test.QuickCheck.Instances.Text ()
Example4 expressions
instance Arbitrary (Salt a) where arbitrary = Salt . pack <$> vector 16instance Arbitrary Password where arbitrary = fmap mkPassword arbitrarylet testParams = defaultParams {argon2TimeCost = 1}let salt = Salt "abcdefghijklmnop"