Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lukegalea16's avatar

Curious about how Hash works

If I hash a Password on one Laravel project, can I use the Hash::check on another Laravel project? How does the Hash encrypt/add salt to the password?

0 likes
7 replies
kingmaker_bgp's avatar

You can share the Hashed passwords for checking in another application. it is basically password_hash('password', PASSWORD_BCRYPT, ['size' => 12]) behind the scenes.

Hash facade doesn't adds any Salt to the Encryption

lukegalea16's avatar

Does the PASSWORD_BCRYPT work the same across different Laravel Projects? Is the APP_KEY used to encrypt the password ?

rodrigo.pedra's avatar

Does the PASSWORD_BCRYPT work the same across different Laravel Projects?

Yes. Bcrypt is actually built-in in PHP, so it will be the same across different Laravel Projects

Is the APP_KEY used to encrypt the password ?

No. Passwords are hashed not encrypted.

When you hash a value, you cannot get the original value back from the hash.

When you encrypt a value, if you have the encryption key you can get the original value back.

Also imagine you need to change your APP_KEY for any reason (employee moves to competitor, .env is leaked, etc.). Every user's password would be invalidated if they are dependent on the APP_KEY.

One last thing: Passwords using Bcrypt in Laravel are salted. It is true Laravel doesn't handle the salt or allow you to define a custom salt, but what happens is that PHP itself will generate a random salt for you.

Reference: https://www.php.net/manual/en/function.password-hash.php

Supported options for PASSWORD_BCRYPT:

  • salt (string) - to manually provide a salt to use when hashing the password. Note that this will override and prevent a salt from being automatically generated.

If omitted, a random salt will be generated by password_hash() for each password hashed. This is the intended mode of operation.

When you see a password hash in the database, for example, using the password string shipped in ./database/factories/UserFactory.php

$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi
| A| B|          C          |               D              |
  • Part A ($2y$) identifies the hashing algorithm (in this case Bcrypt)
  • Part B (10$) identifies the hashing algorithm options, in this case it states this password was hashed using 10 rounds (cost)
  • Part C (92IXUNpkjO0rOQ5byMi.Ye / 22 characters) is the salt. As Laravel doesn't allow you to provide a custom one, a random one is generated
  • Part D (4oKoEa3Ro9llC/.og/at2.uheWG/igi) Is the actual password hash.

When comparing a password, internally Laravel uses PHP's password_verify() which takes a plain string and a password hash to verify if they match.

The way it works is that from the full hash PHP is able to find out the algorithm, options and salt used in the hash. Then it applies the same algorithm, options and salt to the plain string and compare both outputs.

Hope it helps.

EDIT Was curious to see if the . as limiter between the salt and password hash. After not identifying a pattern when verifying some patterns in my local project database, I decided to Google it, and found the the salt, for bcrypt, are the first 22 characters after the algorithm options.

Reference: https://stackoverflow.com/a/40995551

5 likes
lukegalea16's avatar

Thank you Rodrigo, when sending password over an API for authentication with a Laravel application should I add salt to the password before sending? I was considering hashing it before sending such that it already has salt and then just compare both strings in Laravel.

rodrigo.pedra's avatar
Level 56

I wouldn't bother, sending it hashed. If your app is behind SSL (using https://) the connection is encrypted anyway so there is more harm than good on doing that.

If you read that StackOverflow answer I linked the answer's author outlined some valid concerns around picking your own salt and comparing hashes generated by two different implementations (in your case one in PHP and other in JavaScript).

Let's review some of those points

1 - Salt security

...the salt needs to be cryptographically secure (i.e. unpredictable), and PHP already knows how to do that, while chances are (overwhelmingly) that you don't...

Picking a custom salt , or using a fixed salt will defeat the "cryptographically secure" aspect of using a salt, thus decreasing its efficiency.

2 - Base64 encoding differences

The salt gets encoded using base 64 encoding.

There are different base64 encoding techniques and to generate the same salt and hash you will need to be sure you have the same implementation on both your server (PHP) and client (JavaScript).

The link highlights that even in the PHP side the base64 salt encoding yields a different result than using PHP's base64_encode, see this quote:

There are actually a few Base64 "dialects" and the one used by bcrypt is not quite the same as PHP's base64_encode()

Being sure both hashes are generated the same on PHP and JavaScript would require a lot of work to get it right, and to cover any edge cases you might not found on your first implementation.

Bottom line:

If you are using SSL/HTTPS, which you should be using on production, there is not much benefit on hashing before posting a password or other sensitive data.

Actually there are more downsides, if you decide on using a simplified hashing mechanism and, as a malicious actor have access to your client-side JavaScript code (by how web browsers work), you could expose security flaws that could be exploited on your server.

1 like
laracoft's avatar

@lukegalea16

Are you just curious about hashing or is there a bigger question to all these?

  1. Salting is meant to prevent patterns being discovered in the hashes.
  2. Salting is not applicable before sending for authentication.
  3. And NO, you don't want to be hashing before sending, this is literally breaking the protection provided by the hash. Once a hacker gets your hashes, they can login directly your system.

Please state your bigger question or read up on hash and salt so that we can have a more productive conversation about the idea you have in mind.

And if I may add, these concepts are developed by rigorous mathematicians over the decades. It is very unlikely for non-researchers to find better ideas.

2 likes
lukegalea16's avatar

Hi, perhaps you saw some of my other questions. I am authenticating users from this Laravel project against another already existing users table in another Laravel project. Thus I would need to send the credentials over an API to that project. I also created a Custom Auth Guard and User Provider for the application to authenticate against that project's table instead of the existing users table in this project.

Please or to participate in this conversation.