ParagonIE\Halite\Password::hash PHP Method

hash() public static method

Hash then encrypt a password
public static hash ( HiddenString $password, EncryptionKey $secretKey, string $level = KeyFactory::INTERACTIVE ) : string
$password HiddenString The user's password
$secretKey EncryptionKey The master key for all passwords
$level string The security level for this password
return string An encrypted hash to store
    public static function hash(HiddenString $password, EncryptionKey $secretKey, string $level = KeyFactory::INTERACTIVE) : string
    {
        $kdfLimits = KeyFactory::getSecurityLevels($level);
        // First, let's calculate the hash
        $hashed = \Sodium\crypto_pwhash_str($password->getString(), $kdfLimits[0], $kdfLimits[1]);
        // Now let's encrypt the result
        return Crypto::encrypt(new HiddenString($hashed), $secretKey);
    }

Usage Example

Exemplo n.º 1
0
 public function testEncrypt()
 {
     $key = new EncryptionKey(\str_repeat('A', 32));
     $hash = Password::hash('test password', $key);
     $this->assertTrue(is_string($hash));
     $this->assertTrue(Password::verify('test password', $hash, $key));
     $this->assertFalse(Password::verify('wrong password', $hash, $key));
 }
All Usage Examples Of ParagonIE\Halite\Password::hash