forked from nette/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswords.php
More file actions
73 lines (56 loc) · 1.46 KB
/
Copy pathPasswords.php
File metadata and controls
73 lines (56 loc) · 1.46 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
71
72
73
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Security;
use Nette;
/**
* Password Hashing.
*/
class Passwords
{
use Nette\SmartObject;
/** @var int */
private $algo;
/** @var array */
private $options;
/**
* See http://php.net/manual/en/password.constants.php
*/
public function __construct(int $algo = PASSWORD_DEFAULT, array $options = [])
{
$this->algo = $algo;
$this->options = $options;
}
/**
* Computes salted password hash.
*/
public function hash(string $password): string
{
$hash = isset($this)
? @password_hash($password, $this->algo, $this->options) // @ is escalated to exception
: @password_hash($password, PASSWORD_BCRYPT, func_get_args()[1] ?? []); // back compatibility with v2.x
if (!$hash) {
throw new Nette\InvalidStateException('Computed hash is invalid. ' . error_get_last()['message']);
}
return $hash;
}
/**
* Verifies that a password matches a hash.
*/
public function verify(string $password, string $hash): bool
{
return password_verify($password, $hash);
}
/**
* Checks if the given hash matches the options.
*/
public function needsRehash(string $hash): bool
{
return isset($this)
? password_needs_rehash($hash, $this->algo, $this->options)
: password_needs_rehash($hash, PASSWORD_BCRYPT, func_get_args()[1] ?? []); // back compatibility with v2.x
}
}