forked from nette/security
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPasswords.php
More file actions
65 lines (51 loc) · 1.32 KB
/
Copy pathPasswords.php
File metadata and controls
65 lines (51 loc) · 1.32 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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Security;
use Nette;
/**
* Passwords tools.
*/
class Passwords
{
use Nette\StaticClass;
/** @deprecated */
const BCRYPT_COST = 10;
/**
* Computes salted password hash.
* @param string
* @param array with cost (4-31)
* @return string 60 chars long
*/
public static function hash($password, array $options = [])
{
if (isset($options['cost']) && ($options['cost'] < 4 || $options['cost'] > 31)) {
throw new Nette\InvalidArgumentException("Cost must be in range 4-31, $options[cost] given.");
}
$hash = password_hash($password, PASSWORD_BCRYPT, $options);
if ($hash === FALSE || strlen($hash) < 60) {
throw new Nette\InvalidStateException('Hash computed by password_hash is invalid.');
}
return $hash;
}
/**
* Verifies that a password matches a hash.
* @return bool
*/
public static function verify($password, $hash)
{
return password_verify($password, $hash);
}
/**
* Checks if the given hash matches the options.
* @param string
* @param array with cost (4-31)
* @return bool
*/
public static function needsRehash($hash, array $options = [])
{
return password_needs_rehash($hash, PASSWORD_BCRYPT, $options);
}
}