1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Security; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Security\PasswordEncryptor; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class PasswordEncryptor_BCrypt |
9
|
|
|
* @package Dynamic\FoxyStripe\Security |
10
|
|
|
*/ |
11
|
|
|
class PasswordEncryptor_BCrypt extends PasswordEncryptor |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Cost of encryption. |
15
|
|
|
* Higher costs will increase security, but also increase server load. |
16
|
|
|
* If you are using basic auth, you may need to decrease this as encryption |
17
|
|
|
* will be run on every request. |
18
|
|
|
* The two digit cost parameter is the base-2 logarithm of the iteration |
19
|
|
|
* count for the underlying Blowfish-based hashing algorithmeter and must |
20
|
|
|
* be in range 04-31, values outside this range will cause crypt() to fail. |
21
|
|
|
*/ |
22
|
|
|
protected static $cost = 10; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sets the cost of the blowfish algorithm. |
26
|
|
|
* See {@link PasswordEncryptor_Blowfish::$cost} |
27
|
|
|
* Cost is set as an integer but |
28
|
|
|
* Ensure that set values are from 4-31 |
29
|
|
|
* |
30
|
|
|
* @param int $cost range 4-31 |
31
|
|
|
*/ |
32
|
|
|
public static function set_cost($cost) |
33
|
|
|
{ |
34
|
|
|
self::$cost = max(min(31, $cost), 4); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets the cost that is set for the PASSWORD_BCRYPT algorithm |
39
|
|
|
* |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
|
|
public static function get_cost() |
43
|
|
|
{ |
44
|
|
|
return self::$cost; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param String $password |
49
|
|
|
* @param null $member |
|
|
|
|
50
|
|
|
* @return bool|string |
51
|
|
|
*/ |
52
|
|
|
public function encrypt($password, $salt = null, $member = null) |
53
|
|
|
{ |
54
|
|
|
$encryptedPassword = password_hash($password, PASSWORD_BCRYPT, ['cost' => static::get_cost()]); |
55
|
|
|
|
56
|
|
|
if (strpos($encryptedPassword, '$2y$') === false) { |
57
|
|
|
throw new PasswordEncryptor_EncryptionFailed('BCrypt password encryption failed.'); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $encryptedPassword; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $hash |
65
|
|
|
* @param string $password |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function check($hash, $password, $salt = null, $member = null) |
69
|
|
|
{ |
70
|
|
|
return password_verify($password, $hash); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|