Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/src/UserBundle/Security/Hasher/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/UserBundle/Security/Hasher/MemberPortalPasswordHasher.php

<?php

namespace App\UserBundle\Security\Hasher;

use Symfony\Component\PasswordHasher\PasswordHasherInterface;

/**
 * Verifies member portal passwords: plain text (legacy portal) or crypt hash in password column.
 */
final class MemberPortalPasswordHasher implements PasswordHasherInterface
{
    public function hash(string $plainPassword): string
    {
        return $plainPassword;
    }

    public function verify(string $hashedPassword, string $plainPassword): bool
    {
        if ($hashedPassword === '') {
            return false;
        }

        if (hash_equals($hashedPassword, $plainPassword)) {
            return true;
        }

        if ($this->looksLikeCryptHash($hashedPassword)) {
            $check = crypt($plainPassword, $hashedPassword);

            return is_string($check) && hash_equals($hashedPassword, $check);
        }

        return false;
    }

    /**
     * When salt is stored separately (admin registration), verify crypt(plain, salt).
     */
    public function verifyWithSalt(?string $salt, string $hashedPassword, string $plainPassword): bool
    {
        if ($this->verify($hashedPassword, $plainPassword)) {
            return true;
        }

        if ($salt !== null && $salt !== '') {
            $check = crypt($plainPassword, $salt);

            return is_string($check) && hash_equals($hashedPassword, $check);
        }

        return false;
    }

    public function needsRehash(string $hashedPassword): bool
    {
        return false;
    }

    private function looksLikeCryptHash(string $hashedPassword): bool
    {
        return str_starts_with($hashedPassword, '$') || str_starts_with($hashedPassword, '*');
    }
}