Download the PHP package units/password without Composer
On this page you can find all versions of the php package units/password. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download units/password
More information about units/password
Files in units/password
Package password
Short Description Password-hashing unit - Argon2id, Argon2i and bcrypt via PHP's password_* functions, on the patterns/unit contract.
License MIT
Informations about the package password
Units — Password
Store passwords so they can be checked, and never read back.
PHP's own password_hash(), password_verify() and password_needs_rehash().
The cryptography is the one your PHP was already built and tested with.
The words
Password hash — one way, and slow on purpose, so guessing is expensive.
Salt — random input inside the hash, so the same password gives a different result every time. Automatic here, and the reason one cracked password does not open every account that shares it.
Pepper — a secret mixed into the password before hashing, kept out of the database. Not here: it is your secret and your decision.
Cost — how much work the hash does. Higher is slower, and stronger.
Storing
One line of text, carrying its own algorithm, its own settings and its own salt.
Store it in a VARCHAR(255).
The same password gives a different string every time. Never store a constant, never compare two stored values, never expect them to match.
Checking
The only way to check. It reads the algorithm out of the stored value, not out of your configuration, so one unit checks today's Argon2id hashes and 2019's bcrypt hashes alike.
verify() never throws. An empty, truncated or nonsense value is simply false:
Upgrading stored passwords
Settings get stronger over time. You can only re-hash while the password is in hand — right after a successful check:
Nobody resets anything. Each user is upgraded the next time they sign in.
What a stored hash says about itself
The setting that does nothing
Settings go to PHP unchanged, so a setting from the wrong algorithm is silently ignored — by PHP, and therefore by you:
cost is bcrypt's. Argon2id has none, so that is not a stronger Argon2id — it
is an ordinary one, and needsRehash() will not notice either. Ask the hash
instead of trusting the request:
Argon2id takes memory_cost, time_cost, threads. bcrypt takes cost.
Prefer Argon2id
Argon2id is optional in PHP, so the unit refuses an algorithm this build cannot run — when you configure it, not at a signup:
Use bcrypt when something else has to read the value.
The pepper is yours
A pepper never enters the database, so stealing the table is not enough on its own. Where it comes from, how it rotates, whether you want one — your call, not a library's. This unit hashes what it is handed.
Not the hasher
units/hasher fingerprints data: fast, always the same answer. This protects
credentials: slow, never the same answer. Each refuses to be the other —
Password has no hmac() or sha256(), Hasher has no needsRehash().
It knows its version
Reference
| Method | Gives you |
|---|---|
create(['algorithm' => …, 'options' => …, 'dna' => …]) |
a password unit |
hash($password) |
PasswordResult — store ->hash |
verify($password, $stored) |
bool, never throws |
needsRehash($stored) |
bool — is this behind the settings |
algorithms() supports($name) |
what this PHP can actually run |
algorithm() options() dna() whoami() help() |
itself |
PasswordResult carries hash, algorithm and options. It is immutable, and
(string) $result is the stored value.
MIT · tests: composer test