1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Hustoj\Hashing; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Hashing\Hasher as HashingContract; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
class Hasher implements HashingContract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Check the given plain value against a hash. |
12
|
|
|
* |
13
|
|
|
* @param string $value |
14
|
|
|
* @param string $hashedValue |
15
|
|
|
* @param array $options |
16
|
|
|
* |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
public function check($value, $hashedValue, array $options = []) |
20
|
|
|
{ |
21
|
|
|
// TODO: Implement check() method. |
22
|
|
|
if (md5($value) == $hashedValue) { |
23
|
|
|
// generate new style password |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$origin_hash = base64_decode($hashedValue); |
27
|
|
|
$salt = substr($origin_hash, 20); |
28
|
|
|
|
29
|
|
|
$hashed_password = $this->make($value, ['salt' => $salt]); |
30
|
|
|
|
31
|
|
|
return $hashed_password == $hashedValue; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Hash the given value. |
36
|
|
|
* |
37
|
|
|
* @param string $value |
38
|
|
|
* @param array $options |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function make($value, array $options = []) |
43
|
|
|
{ |
44
|
|
|
$salt = Arr::get($options, 'salt'); |
45
|
|
|
if (! $salt) { |
46
|
|
|
$this->generateSalt(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$hashed_password = sha1(md5($value).$salt, true); |
50
|
|
|
|
51
|
|
|
return base64_encode($hashed_password.$salt); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function generateSalt() |
55
|
|
|
{ |
56
|
|
|
$salt = sha1(mt_rand()); |
57
|
|
|
|
58
|
|
|
return substr($salt, 0, 4); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if the given hash has been hashed using the given options. |
63
|
|
|
* |
64
|
|
|
* @param string $hashedValue |
65
|
|
|
* @param array $options |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function needsRehash($hashedValue, array $options = []) |
70
|
|
|
{ |
71
|
|
|
//todo: rehash old password to new password |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get information about the given hashed value. |
77
|
|
|
* |
78
|
|
|
* @param string $hashedValue |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function info($hashedValue) |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
'algo' => 0, |
86
|
|
|
'algoName' => 'hustoj', |
87
|
|
|
'options' => [], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* detect password is old md5 password. |
93
|
|
|
* |
94
|
|
|
* @param string $password |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
protected function isDeprecated($password) |
99
|
|
|
{ |
100
|
|
|
$len = strlen($password); |
101
|
|
|
for ($pos = 0; $pos < $len; $pos++) { |
102
|
|
|
$char = $password[$pos]; |
103
|
|
|
if ($this->isCharactorInHexScope($char)) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
break; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* detect charactor is 0-9a-z. |
114
|
|
|
* |
115
|
|
|
* @param $char |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
private function isCharactorInHexScope($char) |
120
|
|
|
{ |
121
|
|
|
$char = strtolower($char); |
122
|
|
|
if (ctype_digit($char)) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
if ($char >= 'a' && $char <= 'z') { |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|