Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel how to create sha256 hash with salt

I have a running application that uses client-side Sha256 hashing.
I would like to use Laravels serverside bcrypt hashing instead.

My strategy is to wrap all passwords with bcrypt, so I have bcrypt(sha256('password')), and then rehash the password when the user attempts to log in, so I simply have bcrypt('password').

My problem is authenticating the user when they try to log in with a Sha256 password.

I try to authenticate them by running
if (hash('sha256', 'password' . 'salt') == $stored_pw)
But with no luck. I'm only fairly certain that the client-side hashing simply appends the salt, and I'm unsure if Laravels hash function adds a salt of its own.

Here's a hash created by the client from the password 1234567: $5$a0FpUG9JUgkj1d6H$eSSzXebYU87wPAWSTRJGyWw/kOMgDvPqcri4CI1QCV0
I am trying to recreate the same hash using the salt, the password, and Laravels hashing functions.

How do I specify that the Sha256 function should use a specific salt?

like image 498
T.Palludan Avatar asked Oct 30 '25 01:10

T.Palludan


2 Answers

Try.

use phpseclib\Crypt\Hash;
or
use Hash

\Hash::make($request->password);

or

$hash = Hash::make('secret');

$input = 'secret';
if(Hash::check($input, $hash)){
    // the input matches the secret
}
like image 147
VIKAS KATARIYA Avatar answered Oct 31 '25 21:10

VIKAS KATARIYA


Laravel has dedicated Facade Support for this:

use Illuminate\Support\Facades\Hash;


$hash = Hash::make($string);

if (Hash::check($stringYouWantToCompare, $hash)) {
    return true; // Valid
}else {
    return false; // Invalid
}
like image 26
Nazmul Hasan Avatar answered Oct 31 '25 22:10

Nazmul Hasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!