-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhash.php
53 lines (46 loc) · 1.17 KB
/
hash.php
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
<?php namespace Laravel;
class Hash {
/**
* Hash a password using the Bcrypt hashing scheme.
*
* <code>
* // Create a Bcrypt hash of a value
* $hash = Hash::make('secret');
*
* // Use a specified number of iterations when creating the hash
* $hash = Hash::make('secret', 12);
* </code>
*
* @param string $value
* @param int $rounds
* @return string
*/
public static function make($value, $rounds = 8)
{
$work = str_pad($rounds, 2, '0', STR_PAD_LEFT);
// Bcrypt expects the salt to be 22 base64 encoded characters including
// dots and slashes. We will get rid of the plus signs included in the
// base64 data and replace them with dots.
if (function_exists('openssl_random_pseudo_bytes'))
{
$salt = openssl_random_pseudo_bytes(16);
}
else
{
$salt = Str::random(40);
}
$salt = substr(strtr(base64_encode($salt), '+', '.'), 0 , 22);
return crypt($value, '$2a$'.$work.'$'.$salt);
}
/**
* Determine if an unhashed value matches a Bcrypt hash.
*
* @param string $value
* @param string $hash
* @return bool
*/
public static function check($value, $hash)
{
return crypt($value, $hash) === $hash;
}
}