-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathPasswords.php
More file actions
70 lines (57 loc) · 1.52 KB
/
Copy pathPasswords.php
File metadata and controls
70 lines (57 loc) · 1.52 KB
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
<?php declare(strict_types=1);
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Security;
use Nette;
/**
* Password hashing and verification.
*/
class Passwords
{
/**
* Configures the hashing algorithm and its options.
*/
public function __construct(
private readonly string $algo = PASSWORD_DEFAULT,
/** @var array<string, mixed> algorithm-specific options, see https://php.net/manual/en/password.constants.php */
private readonly array $options = [],
) {
}
/**
* Computes a password hash containing the algorithm ID, settings, salt, and the hash itself.
*/
public function hash(
#[\SensitiveParameter]
string $password,
): string
{
if ($password === '') {
throw new Nette\InvalidArgumentException('Password can not be empty.');
}
$hash = @password_hash($password, $this->algo, $this->options); // @ is escalated to exception
if (!$hash) {
throw new Nette\InvalidStateException('Computed hash is invalid. ' . (error_get_last()['message'] ?? ''));
}
return $hash;
}
/**
* Checks whether the password matches the given hash.
*/
public function verify(
#[\SensitiveParameter]
string $password,
string $hash,
): bool
{
return password_verify($password, $hash);
}
/**
* Checks whether the hash needs to be rehashed with the current algorithm and options.
*/
public function needsRehash(string $hash): bool
{
return password_needs_rehash($hash, $this->algo, $this->options);
}
}