-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.php
81 lines (68 loc) · 2.23 KB
/
Hash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use Tamedevelopers\Support\Env;
use Tamedevelopers\Support\Capsule\Manager;
use Tamedevelopers\Support\Capsule\CustomException;
final class Hash {
/**
* Password Encrypter.
* This function encrypts a password using bcrypt with a generated salt.
*
* @param string $password
* - The password to encrypt.
*
* @return string
* - The encrypted password.
*/
static public function make($password)
{
// Check if the password exceeds the maximum length
self::passwordLengthVerifier($password, 72);
// Hash the password using bcrypt with the generated salt
return password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
}
/**
* Password Verifier.
* This function verifies a new password against the old hashed password.
*
* @param string $newPassword
* - The new password to verify.
*
* @param string $oldHashedPassword
* - The old hashed password to verify against.
*
* @return bool
* - Returns true if the verification is successful, false otherwise.
*/
static public function check($newPassword, $oldHashedPassword)
{
return password_verify($newPassword, $oldHashedPassword);
}
/**
* Throw error if password more than maximum allowed legnth
*
* @param mixed $password
* @param mixed $maxPasswordLength
* @return void
*/
static private function passwordLengthVerifier($password, $maxPasswordLength = 72)
{
try {
if (mb_strlen($password, 'UTF-8') > $maxPasswordLength) {
throw new CustomException(
"Password exceeds the maximum allowed length of {$maxPasswordLength} bytes."
);
}
} catch (CustomException $e) {
// Handle the exception silently (turn off error reporting)
error_reporting(0);
Manager::setHeaders(404, function() use($e){
// create error logger
Env::bootLogger();
// Trigger a custom error
trigger_error($e->getMessage(), E_USER_ERROR);
});
}
}
}