I am currently working on setting up a custom SHA-256 implementation for password handling. While the functionality works as expected for account registration, I encounter an issue during the login process, where the system returns an error indicating that the credentials are incorrect or do not match.
In the check() function $hashedValue returns as Resource id #
have i misunderstood how to implement to Hasher class? any tips are welcome
[2025-01-21 11:32:29] local.INFO: check {"value":"blablabla","hashedValue":"Resource id #7","hashedValueType":"string","hashedValueContent":"Resource id #7"}
app/Hashers/Sha256Hasher.php
<?php
namespace App\Hashers;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Hashing\Hasher;
class Sha256Hasher implements Hasher
{
public function make($value, array $options = [])
{
return hash('sha256', $value);
}
public function check($value, $hashedValue, array $options = [])
{
Log::info('check', [
'value' => $value,
'hashedValue' => $hashedValue,
'hashedValueType' => gettype($hashedValue),
'hashedValueContent' => is_resource($hashedValue) ? stream_get_contents($hashedValue) : $hashedValue,
]);
return hash('sha256', $value) === $hashedValue;
}
public function needsRehash($hashedValue, array $options = [])
{
return false;
}
public function info($hashedValue)
{
return [
'algo' => 'sha256',
'algoName' => 'sha256',
'options' => [],
];
}
public function verifyConfiguration()
{
if (!extension_loaded('hash')) {
throw new \RuntimeException('The hash extension is not available on your server.');
}
if (!in_array('sha256', hash_algos())) {
throw new \RuntimeException('SHA-256 is not supported on this server.');
}
}
}
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Hash;
use App\Hashers\Sha256Hasher;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// define password encrypt method
$hashingDriver = config('hashing.driver', 'sha256');
if ($hashingDriver === 'sha256') {
Hash::extend('sha256', function () {
return new Sha256Hasher();
});
}
Vite::prefetch(concurrency: 3);
}
}
config/hashing.php
<?php
return [
'driver' => env('HASHING_DRIVER', 'sha256'),
'bcrypt' => [
'rounds' => 10,
],
'argon' => [
'memory' => 1024,
'time' => 2,
'threads' => 2,
],
'argon2id' => [
'memory' => 1024,
'time' => 2,
'threads' => 2,
],
// 'custom' => [
'sha256' => [
'driver_class' => App\Hashers\Sha256Hasher::class,
'options' => [],
],
];