Habari\Utils::ssha512 PHP Méthode

ssha512() public static méthode

Implements a modified version of the {Seeded,Salted}-SHA algorithm from RfC 2307, using SHA-512 instead of SHA-1. Requires the new hash*() functions.
public static ssha512 ( string $password, string $hash = null ) : string
$password string the password to crypt or verify
$hash string (optional) if given, verify $password against $hash
Résultat string encrypted password, or boolean for verification
    public static function ssha512($password, $hash = null)
    {
        $marker = '{SSHA512}';
        if ($hash == null) {
            // encrypt
            $salt = '';
            for ($i = 0; $i < 4; $i++) {
                $salt .= chr(mt_rand(0, 255));
            }
            $digest = hash('sha512', $password . $salt, true);
            return $marker . base64_encode($digest . $salt);
        } else {
            // verify
            if (!substr($hash, 0, strlen($marker)) == $marker) {
                Error::raise(_t('Invalid hash'));
                return false;
            }
            $hash = substr($hash, strlen($marker));
            $hash = base64_decode($hash);
            $digest = substr($hash, 0, 64);
            $salt = substr($hash, 64);
            return hash('sha512', $password . $salt, true) == $digest;
        }
    }