Password storage as PHC strings (Effect's effect/crypto/Password)::
passwords = yield from Password()
stored = yield from passwords.hash("hunter2")
valid = yield from passwords.verify("hunter2", stored)The Password service hashes with one configured algorithm, Argon2id or
PBKDF2 from kdf, into a string such as
$argon2id$v=19$m=65536,t=3,p=4$<salt>$<hash> that records the algorithm,
its costs and the salt. verify reads them back, so hashes made under an
older policy keep verifying, and needs_rehash says when to replace them.
decode and encode convert between PHC strings and PasswordHash
values without a service.
Passwords are UTF-8 encoded without Unicode normalization, as other PHC
implementations do. verify runs whatever costs the stored hash states, so
decode a hash from an untrusted source and check its algorithm first.
Argon2id needs a Kdf layer that provides it, such as native.layer_kdf.
Re-exports#
Types#
| Type | Definition | Description |
|---|---|---|
Algorithm | type Algorithm = Argon2id | Pbkdf2 | A password hashing algorithm: Argon2id or PBKDF2. |
Classes#
Functions#
decode#
decode(phc) -> Effect[PasswordHash, CryptoError]Parses a PHC string.
Accepts $argon2id$v=19$m=<memory>,t=<passes>,p=<parallelism>$<salt>$<hash>
and $pbkdf2-<sha1|sha256|sha384|sha512>$i=<iterations>[,l=<length>]$<salt>$<hash>,
where l, when present, must equal the hash length. Salt and hash are
Base64 without padding, and numbers are safe integers in decimal without
leading zeros. Anything else, including other Argon2 versions and variants,
bcrypt and scrypt, fails with an InvalidArgument reason. Costs are
range-checked when deriving, not here.
| Parameter | Type | Description |
|---|---|---|
phc | str |
Returns Effect[PasswordHash, CryptoError]
def decode(phc: str) -> Effect[PasswordHash, CryptoError]:
"""Parses a PHC string.
Accepts ``$argon2id$v=19$m=<memory>,t=<passes>,p=<parallelism>$<salt>$<hash>``
and ``$pbkdf2-<sha1|sha256|sha384|sha512>$i=<iterations>[,l=<length>]$<salt>$<hash>``,
where ``l``, when present, must equal the hash length. Salt and hash are
Base64 without padding, and numbers are safe integers in decimal without
leading zeros. Anything else, including other Argon2 versions and variants,
bcrypt and scrypt, fails with an ``InvalidArgument`` reason. Costs are
range-checked when deriving, not here."""
return suspend(lambda: _decode(phc))encode#
encode(hash) -> strFormats hash as a PHC string. PBKDF2 hashes always include l.
| Parameter | Type | Description |
|---|---|---|
hash | PasswordHash |
Returns str
def encode(hash: PasswordHash) -> str:
"""Formats ``hash`` as a PHC string. PBKDF2 hashes always include ``l``."""
length = f",l={len(hash.hash)}" if isinstance(hash.algorithm, Pbkdf2) else ""
return f"${_params(hash.algorithm)}{length}${_b64_encode(hash.salt)}${_b64_encode(hash.hash)}"layer#
layer(*, algorithm) -> Layer[Password, Never, Kdf | SecureRandom]Provides Password, hashing new passwords with algorithm.
There is no default algorithm. kdf.argon2id() and kdf.pbkdf2_sha256
carry recommended costs; Argon2id fails with an Unsupported reason from
Kdf when the Kdf layer does not provide it.
| Parameter | Type | Description |
|---|---|---|
algorithm | Algorithm |
Returns Layer[Password, Never, Kdf | SecureRandom]
def layer(*, algorithm: Algorithm) -> Layer[Password, Never, Kdf | SecureRandom]:
"""Provides ``Password``, hashing new passwords with ``algorithm``.
There is no default algorithm. ``kdf.argon2id()`` and ``kdf.pbkdf2_sha256``
carry recommended costs; Argon2id fails with an ``Unsupported`` reason from
``Kdf`` when the ``Kdf`` layer does not provide it."""
@gen
def build():
kdf = yield from Kdf()
random = yield from SecureRandom()
return _Service(algorithm, kdf, random)
return layer_effect(Password, build)