Skip to content

Core\Password::hash

Turns a password into a hash that you can store in a database.

You never store the password itself. You store the string hash returns, and later you give it to Core\Password::verify to check a password somebody types. The string starts with $argon2id$ and contains the settings, a random salt and the hash itself, so verify needs nothing else.

hash has no algorithm or cost argument. The library chooses safe settings. When those settings change, Core\Password::needsRehash tells you which stored hashes are older.

Each call uses a new random salt. The same password gives a different string every time, so two users with the same password do not have the same hash.

Good to know: one call uses 19 MiB of memory for a short time. This is on purpose: it makes guessing passwords slow and expensive.

The examples below hash a password, show that two hashes of one password differ, and store a new user's password at sign-up.

Parameters

  • $passwordstringThe password to hash. It may be a secret string. The result is not secret, because it is made to be stored.

Return value

Returns string. A string such as $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>. Each call uses a new random salt, so the same password gives a different string every time.

Errors

  • RuntimeErrorThe request cannot use the 19 MiB of memory that one hash needs.

Examples

Hash a new password

<?nvs
// Shows how to turn a password into a hash that you can store.
secret string $password = 'correct horse battery staple';
string $stored = Core\Password::hash($password);
// The hash starts with the name of the algorithm. This prints `$argon2id$`.
echo Core\Str::slice($stored, 0, 10), "\n";
// The rest is a random salt and the hash itself, so it is different on every
// run. Its length is always the same. This prints 97.
echo Core\Str::length($stored), "\n";
// `verify` reads the stored string back. This prints `matches`.
echo Core\Password::verify($password, $stored) ? 'matches' : 'does not match', "\n";
Output
$argon2id$
97
matches

One password two hashes

<?nvs
// Shows that the same password gives a different hash every time, and that
// both hashes still match the password.
secret string $password = 'hunter2';
string $first = Core\Password::hash($password);
string $second = Core\Password::hash($password);
// Each call uses a new random salt. This prints `different`.
echo $first == $second ? 'the same' : 'different', "\n";
// `verify` reads the salt from each hash, so both match. This prints `true`
// twice.
echo Core\Password::verify($password, $first) ? 'true' : 'false', "\n";
echo Core\Password::verify($password, $second) ? 'true' : 'false', "\n";
Output
different
true
true

Store a password at sign up

<?nvs
// Shows how a sign-up form stores a new user's password. Only the hash is
// kept. The password itself is never stored.
class Account
{
public string $email = '';
public string $passwordHash = '';
}
class SignUp
{
public static function create(string $email, secret string $password): Account
{
Account $account = new Account();
$account->email = $email;
$account->passwordHash = Core\Password::hash($password);
return $account;
}
}
// This value comes from the form.
secret string $typed = 'a long and private sentence';
Account $account = SignUp::create('ada@example.com', $typed);
// A real program saves `$account` in its database. This prints the email and
// `$argon2id$`, the start of the stored hash.
echo $account->email, "\n";
echo Core\Str::slice($account->passwordHash, 0, 10), "\n";
// At the next login, the typed password is checked against the stored hash.
// This prints `logged in`.
secret string $atLogin = 'a long and private sentence';
if (Core\Password::verify($atLogin, $account->passwordHash)) {
echo "logged in\n";
}
Output
ada@example.com
$argon2id$
logged in