2022-07-21 16:15:47 +02:00
|
|
|
<?php
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
namespace App\Service;
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
|
|
|
|
use Symfony\Component\PasswordHasher\Hasher\CheckPasswordLengthTrait;
|
|
|
|
use Symfony\Component\PasswordHasher\LegacyPasswordHasherInterface;
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
class PasswordEncoder implements LegacyPasswordHasherInterface
|
|
|
|
{
|
|
|
|
use CheckPasswordLengthTrait;
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
public function hash(string $plainPassword, string $salt = null): string
|
|
|
|
{
|
|
|
|
if ($this->isPasswordTooLong($plainPassword)) {
|
|
|
|
throw new InvalidPasswordException();
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
$hash = '{SSHA}'.base64_encode(pack('H*', sha1($plainPassword.$salt)).$salt);
|
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
return $hash;
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
public function verify(string $hashedPassword, string $plainPassword, string $salt = null): bool
|
|
|
|
{
|
|
|
|
if ('' === $plainPassword || $this->isPasswordTooLong($plainPassword)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
|
|
|
return $this->hash($plainPassword, $salt) === $hashedPassword;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
public function needsRehash(string $hashedPassword): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
}
|