|
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
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.