Passwords
A password prints as ********. That applies to echo, logs, JSON and error messages. It can't be compared with == either, because that would leak timing. Hash it to store it, and verify the hash at sign-in:
<let name="pw" type="password" value="'correct horse battery staple'" />
<echo>Password: { $pw }</echo>
<let name="stored" value="$pw->hash()" />
<echo>{ $stored->starts_with('$argon2id$') } { $pw->verify($stored) }</echo>
<let name="weak" type="password" value="'password1'" />
<echo>{ $weak->strength(min: 12, symbol).problems }</echo>
<echo>{ $weak->is_strong() }</echo>$pw: password = 'correct horse battery staple'
echo "Password: { $pw }"
$stored = $pw->hash()
echo "{ $stored->starts_with('$argon2id$') } { $pw->verify($stored) }"
$weak: password = 'password1'
echo "{ $weak->strength(min: 12, symbol).problems }"
echo "{ $weak->is_strong() }"| Method | What it does |
|---|---|
->hash(algorithm: argon2id) |
A salted hash in PHC format. The default is argon2id; bcrypt (cost 12) works up to 72 bytes. |
->verify(hash) |
True if the password matches the hash, in constant time. Both formats are read. |
->needs_rehash(hash) |
True when the hash was made with other settings, so it can be upgraded at the next sign-in. |
->strength(...) |
{ score, label, ok, problems }. Score 0 to 4 estimates how hard it is to guess. |
->is_strong(...) |
True when strength finds no problems. |
->reveal() |
The text itself, for the rare place that needs it. |
strength and strong take named rules: min (8 by default) and max (length), upper, lower, digit and symbol (each one needed), common: false (allow the most common passwords), not: [...] (words it mustn't contain, such as the user's name) and score (the lowest score that passes, 3 by default). The same rules can be given to the type, as in password(min: 12), and then a password that breaks them can't be made.