-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbcrypt.php
More file actions
149 lines (135 loc) · 2.68 KB
/
bcrypt.php
File metadata and controls
149 lines (135 loc) · 2.68 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* LightPHP Framework
* LitePHP is a framework that has been designed to be lite waight, extensible and fast.
*
* @author Robert Pitt <robertpitt1988@gmail.com>
* @category core
* @copyright 2013 Robert Pitt
* @license GPL v3 - GNU Public License v3
* @version 1.0.0
*/
/**
* Bcrypt class
*
* This class is based on: https://github.com/cosenary/Bcrypt-PHP-Class
*/
class Bcrypt_Library
{
/**
* Work cost factor
* range between 04-31
*
* @var string
*/
private $_workFactor = 8;
/**
* All valid hash identifiers
*
* @var array
*/
private $_validIdentifiers = array ('2a', '2x', '2y');
/**
* Constructor
*/
public function __construct()
{
/**
* Validate the PHP Version is sufficient.
*/
if (version_compare(PHP_VERSION, '5.3') < 0)
{
throw new Exception('Bcrypt requires PHP 5.3 or above');
}
/**
* Validate OpenSSL is installed for php.
*/
if (!function_exists('openssl_random_pseudo_bytes'))
{
throw new Exception('PHP OpenSSL Library is missing.');
}
}
/**
* Hash password
*
* @param string $password
* @param integer $workFactor
* @return string
*/
public function encrypt($password, $workFactor = 0, $identifier = '2y')
{
/**
* Return the hash
*/
return crypt($password, $this->_genSalt($workFactor, $identifier));
}
/**
* Check bcrypt password
*
* @param string $password
* @param string $storedHash
* @return boolean
*/
public function validate($password, $storedHash)
{
/**
* Validate the identifier
*/
try {
$this->_validateIdentifier($storedHash);
}catch(Exception $e)
{
return false;
}
/**
* Validate
*/
return (crypt($password, $storedHash) === $storedHash);
}
private function _genSalt($workFactor, $identifier = '2y')
{
if ($workFactor < 4 || $workFactor > 31)
{
$workFactor = $this->_workFactor;
}
/**
* Generate some random bytes
* @var string
*/
$input = $this->_getRandomBytes();
/**
* Begin the salt with hte initial hash identifier
* @var string
*/
$salt = '$' . $identifier . '$';
/**
* Append the work factor to the hash
*/
$salt .= str_pad($workFactor, 2, '0', STR_PAD_LEFT) . '$';
/**
* Return the salt.
*/
return $salt . substr(strtr(base64_encode($input), '+', '.'), 0, 22);
}
/**
* OpenSSL's random generator
*
* @return string
*/
private function _getRandomBytes()
{
return openssl_random_pseudo_bytes(16);
}
/**
* Validate Identifier
* @param string $hash
* @return void
*/
private function _validateIdentifier($hash)
{
if (!in_array(substr($hash, 1, 2), $this->_validIdentifiers))
{
throw new Exception('Unsupported hash format.');
}
}
}