1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Hash |
5
|
|
|
* |
6
|
|
|
* Hashing shorthands. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class Hash { |
15
|
|
|
use Module; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create ah hash for payload |
19
|
|
|
* @param mixed $payload The payload string/object/array |
20
|
|
|
* @param integer $method The hashing method, default is "md5" |
21
|
|
|
* @return string The hash string |
22
|
|
|
*/ |
23
|
|
|
public static function make($payload,$method='md5'){ |
24
|
|
|
return hash($method,serialize($payload)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Verify if given payload matches hash |
29
|
|
|
* @param mixed $payload The payload string/object/array |
30
|
|
|
* @param string $hash The hash string |
31
|
|
|
* @param integer $method The hashing method |
32
|
|
|
* @return bool Returns `true` if payload matches hash |
33
|
|
|
*/ |
34
|
|
|
public static function verify($payload,$hash,$method='md5'){ |
35
|
|
|
return static::make($payload,$method) == $hash; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* List registered hashing algorithms |
40
|
|
|
* |
41
|
|
|
* @method methods |
42
|
|
|
* |
43
|
|
|
* @return array Array containing the list of supported hashing algorithms. |
44
|
|
|
*/ |
45
|
|
|
public static function methods(){ |
46
|
|
|
return hash_algos(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check if an alghoritm is registered in current PHP |
52
|
|
|
* |
53
|
|
|
* @method can |
54
|
|
|
* |
55
|
|
|
* @param string $algo The hashing algorithm name |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public static function can($algo){ |
60
|
|
|
return in_array($algo,hash_algos()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Static magic for creating hashes with a specified algorithm. |
65
|
|
|
* |
66
|
|
|
* See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
67
|
|
|
*/ |
68
|
|
|
public static function __callStatic($method,$params){ |
69
|
|
|
return self::make(current($params),$method); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function uuid($type=4, $namespace='', $name=''){ |
73
|
|
|
switch($type){ |
74
|
|
View Code Duplication |
case 3: if(preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
75
|
|
|
'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) return false; |
76
|
|
|
$nhex = str_replace(array('-','{','}'), '', $namespace); |
77
|
|
|
$nstr = ''; for($i = 0; $i < strlen($nhex); $i+=2) |
78
|
|
|
$nstr .= chr(hexdec($nhex[$i].$nhex[$i+1])); |
79
|
|
|
$hash = md5($nstr . $name); |
80
|
|
|
return sprintf('%08s-%04s-%04x-%04x-%12s', |
81
|
|
|
substr($hash, 0, 8), substr($hash, 8, 4), |
82
|
|
|
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
83
|
|
|
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
84
|
|
|
substr($hash, 20, 12)); |
85
|
|
View Code Duplication |
case 5: if(preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
86
|
|
|
'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) return false; |
87
|
|
|
$nhex = str_replace(array('-','{','}'), '', $namespace); |
88
|
|
|
$nstr = ''; for($i = 0; $i < strlen($nhex); $i+=2) |
89
|
|
|
$nstr .= chr(hexdec($nhex[$i].$nhex[$i+1])); |
90
|
|
|
$hash = sha1($nstr . $name); |
91
|
|
|
return sprintf('%08s-%04s-%04x-%04x-%12s', |
92
|
|
|
substr($hash, 0, 8), substr($hash, 8, 4), |
93
|
|
|
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
94
|
|
|
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
95
|
|
|
substr($hash, 20, 12)); |
96
|
|
|
default: case 4: return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
97
|
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
98
|
|
|
mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
99
|
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public static function murmurhash3_int($key, $seed=0){ |
104
|
|
|
$key = (string) $key; |
105
|
|
|
$klen = strlen($key); |
106
|
|
|
$h1 = $seed; |
107
|
|
|
for ($i=0,$bytes=$klen-($remainder=$klen&3) ; $i<$bytes ; ) { |
108
|
|
|
$k1 = ((ord($key[$i]) & 0xff)) |
109
|
|
|
| ((ord($key[++$i]) & 0xff) << 8) |
110
|
|
|
| ((ord($key[++$i]) & 0xff) << 16) |
111
|
|
|
| ((ord($key[++$i]) & 0xff) << 24); |
112
|
|
|
++$i; |
113
|
|
|
$k1 = (((($k1 & 0xffff) * 0xcc9e2d51) |
114
|
|
|
+ ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) * 0xcc9e2d51) & 0xffff) << 16))) |
115
|
|
|
& 0xffffffff; |
116
|
|
|
$k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
117
|
|
|
$k1 = (((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
118
|
|
|
* 0x1b873593) & 0xffff) << 16))) & 0xffffffff; |
119
|
|
|
$h1 ^= $k1; |
120
|
|
|
$h1 = $h1 << 13 | ($h1 >= 0 ? $h1 >> 19 : (($h1 & 0x7fffffff) >> 19) | 0x1000); |
121
|
|
|
$h1b = (((($h1 & 0xffff) * 5) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 5) |
122
|
|
|
& 0xffff) << 16))) & 0xffffffff; |
123
|
|
|
$h1 = ((($h1b & 0xffff) + 0x6b64) + ((((($h1b >= 0 ? $h1b >> 16 : (($h1b & 0x7fffffff) >> 16) | 0x8000)) |
124
|
|
|
+ 0xe654) & 0xffff) << 16)); |
125
|
|
|
} |
126
|
|
|
$k1 = 0; |
127
|
|
|
switch ($remainder) { |
128
|
|
|
case 3: $k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
129
|
|
|
case 2: $k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
130
|
|
|
case 1: $k1 ^= (ord($key[$i]) & 0xff); |
131
|
|
|
$k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
132
|
|
|
* 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
133
|
|
|
$k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
134
|
|
|
$k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
135
|
|
|
* 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
136
|
|
|
$h1 ^= $k1; |
137
|
|
|
} |
138
|
|
|
$h1 ^= $klen; |
139
|
|
|
$h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
140
|
|
|
$h1 = ((($h1 & 0xffff) * 0x85ebca6b) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
141
|
|
|
* 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; |
142
|
|
|
$h1 ^= ($h1 >= 0 ? $h1 >> 13 : (($h1 & 0x7fffffff) >> 13) | 0x40000); |
143
|
|
|
$h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
144
|
|
|
* 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; |
145
|
|
|
$h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
146
|
|
|
return $h1; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public static function murmurhash3($key, $seed=0){ |
150
|
|
|
return base_convert(static::murmurhash3_int($key, $seed),10,32); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|