1
0
mirror of https://github.com/delight-im/PHP-Auth.git synced 2025-10-24 12:26:05 +02:00

Extract usages of hashing for passwords to new class 'PasswordHash'

This commit is contained in:
Marco
2025-05-17 18:19:07 +02:00
parent 6b7ef7c93c
commit 3625622670
3 changed files with 51 additions and 5 deletions

46
src/PasswordHash.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
/*
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
namespace Delight\Auth;
final class PasswordHash {
const HASH_ALGORITHM_IDENTIFIER = \PASSWORD_DEFAULT;
/**
* Creates a computationally expensive hash from a password
*
* @param string $passwordText
* @return string|bool
*/
public static function from($passwordText) {
return \password_hash($passwordText, self::HASH_ALGORITHM_IDENTIFIER);
}
/**
* Verifies whether a password matches a computationally expensive hash
*
* @param string $passwordText
* @param string $expectedHash
* @return bool
*/
public static function verify($passwordText, $expectedHash) {
return \password_verify($passwordText, $expectedHash);
}
/**
* Checks whether a computationally expensive hash needs to be updated to match a desired algorithm and set of options
*
* @param string $existingHash
* @return bool
*/
public static function needsRehash($existingHash) {
return \password_needs_rehash($existingHash, self::HASH_ALGORITHM_IDENTIFIER);
}
}