What is hashing
Hashing is a process that transforms plaintext data (such as passwords) into a fixed-length string via a one-way transformation. The same input always produces the same hash, but you cannot recover the original plaintext from the hash. Laravel’sHash facade supports the bcrypt and Argon2 hashing algorithms for securely storing passwords.
Algorithm comparison
The bcrypt “work factor” controls the time it takes to generate a hash. The slower the hash, the greater the resistance to brute-force attacks. As hardware gets faster, you can maintain security by increasing the work factor.
Hashing and verification flow
Configuration
By default, Laravel uses thebcrypt driver. You can change it via the HASH_DRIVER environment variable.
config:publish.
config/hashing.php.
Basic usage
Hashing a password
Passing the plaintext password toHash::make() returns a hash value. Store this hash value in the database.
Adjusting the bcrypt work factor
You can adjust the compute cost of hash generation via therounds option. A larger value is more secure but also takes longer to process.
The default value (12) is appropriate for most applications.
Adjusting Argon2 parameters
When using Argon2, you can adjust the compute cost via thememory, time, and threads options.
Verifying passwords
UseHash::check() to verify whether a plaintext password matches a stored hash.
This is a typical use in login handling.
Auth::attempt(), this is done automatically, so you don’t need to call it directly.
Hash::check() is useful when you need to manually verify the current password.
Determining when to rehash
UseHash::needsRehash() to check whether a hash was generated with a work factor different from the current configuration.
Use it to update existing hashes to the new configuration after changing the work factor.
Hash algorithm verification
By default,Hash::check() verifies that a hash was generated by the currently configured algorithm.
If the algorithm is different, a RuntimeException is thrown.
This prevents attacks that tamper with the hash algorithm.
When migrating algorithms and needing to support multiple algorithms simultaneously, you can disable this verification by setting HASH_VERIFY to false.
Summary
Next steps
Authentication introduction
Learn the full picture of authentication, including implementing login handling using
Hash::check().