Completed
Push — master ( 4a7f99...ceaa03 )
by Stefano
03:53
created

Hash::murmur()   F

Complexity

Conditions 20
Paths > 20000

Size

Total Lines 46
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 42
c 1
b 0
f 0
nc 104000
nop 3
dl 0
loc 46
rs 2.6285

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
class Hash {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
	use Module;
15
16
	/**
17
	 * Create ah hash for payload
18
	 * @param  mixed $payload The payload string/object/array
19
	 * @param  integer $method  The hashing method, default is "md5"
20
	 * @return string          The hash string
21
	 */
22
	public static function make($payload, $method = 'md5') {
23
		return $method == 'murmur' ? static::murmur(serialize($payload)) : hash($method, serialize($payload));
24
	}
25
26
	/**
27
	 * Verify if given payload matches hash
28
	 * @param  mixed $payload  The payload string/object/array
29
	 * @param  string $hash    The hash string
30
	 * @param  integer $method The hashing method
31
	 * @return bool            Returns `true` if payload matches hash
32
	 */
33
	public static function verify($payload, $hash, $method = 'md5') {
34
		return static::make($payload, $method) == $hash;
35
	}
36
37
	/**
38
	 * List registered hashing algorithms
39
	 *
40
	 * @method methods
41
	 *
42
	 * @return array   Array containing the list of supported hashing algorithms.
43
	 */
44
	public static function methods() {
45
    // Merge PHP provided algos with ours (murmur)
46
		return array_merge(hash_algos(), ['murmur','murmurhash3']);
47
	}
48
49
	/**
50
	 * Check if an alghoritm is registered in current PHP
51
	 *
52
	 * @method can
53
	 *
54
	 * @param  string $algo The hashing algorithm name
55
	 *
56
	 * @return bool
57
	 */
58
	public static function can($algo) {
59
    // Faster than : in_array(explode(',',implode(',',static::methods())))
60
		return strpos(implode(',',static::methods()).',', "$algo,") !== false;
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}\-?' .
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
75
				'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) {
76
				return false;
77
			}
78
79
			$nhex = str_replace(array('-', '{', '}'), '', $namespace);
80
			$nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) {
81
				$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
82
			}
83
84
			$hash = md5($nstr . $name);
85
			return sprintf('%08s-%04s-%04x-%04x-%12s',
86
				substr($hash, 0, 8), substr($hash, 8, 4),
87
				(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
88
				(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
89
				substr($hash, 20, 12));
90 View Code Duplication
		case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
91
				'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) {
92
				return false;
93
			}
94
95
			$nhex = str_replace(array('-', '{', '}'), '', $namespace);
96
			$nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) {
97
				$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
98
			}
99
100
			$hash = sha1($nstr . $name);
101
			return sprintf('%08s-%04s-%04x-%04x-%12s',
102
				substr($hash, 0, 8), substr($hash, 8, 4),
103
				(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
104
				(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
105
				substr($hash, 20, 12));
106
		default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
107
				mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
108
				mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
109
				mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
110
		}
111
	}
112
113
  public static function murmur($key, $seed = 0, $as_integer=false) {
114
		$key = (string) $key;
115
		$klen = strlen($key);
116
		$h1 = $seed;
117
		for ($i = 0, $bytes = $klen - ($remainder = $klen & 3); $i < $bytes;) {
118
			$k1 = ((ord($key[$i]) & 0xff))
119
			 | ((ord($key[++$i]) & 0xff) << 8)
120
			 | ((ord($key[++$i]) & 0xff) << 16)
121
			 | ((ord($key[++$i]) & 0xff) << 24);
122
			++$i;
123
			$k1 = (((($k1 & 0xffff) * 0xcc9e2d51)
124
				 + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) * 0xcc9e2d51) & 0xffff) << 16)))
125
			 & 0xffffffff;
126
			$k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000);
127
			$k1 = (((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000))
128
				 * 0x1b873593) & 0xffff) << 16))) & 0xffffffff;
129
			$h1 ^= $k1;
130
			$h1 = $h1 << 13 | ($h1 >= 0 ? $h1 >> 19 : (($h1 & 0x7fffffff) >> 19) | 0x1000);
131
			$h1b = (((($h1 & 0xffff) * 5) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 5)
132
				 & 0xffff) << 16))) & 0xffffffff;
133
			$h1 = ((($h1b & 0xffff) + 0x6b64) + ((((($h1b >= 0 ? $h1b >> 16 : (($h1b & 0x7fffffff) >> 16) | 0x8000))
134
				 + 0xe654) & 0xffff) << 16));
135
		}
136
		$k1 = 0;
137
		switch ($remainder) {
138
		case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
139
		case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
140
		case 1:$k1 ^= (ord($key[$i]) & 0xff);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
141
			$k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000))
142
				 * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff;
143
			$k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000);
144
			$k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000))
145
				 * 0x1b873593) & 0xffff) << 16)) & 0xffffffff;
146
			$h1 ^= $k1;
147
		}
148
		$h1 ^= $klen;
149
		$h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000);
150
		$h1 = ((($h1 & 0xffff) * 0x85ebca6b) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000))
151
			 * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
152
		$h1 ^= ($h1 >= 0 ? $h1 >> 13 : (($h1 & 0x7fffffff) >> 13) | 0x40000);
153
		$h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000))
154
			 * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
155
		$h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000);
156
157
		return $as_integer ? $h1 : base_convert($h1 ,10, 32);
158
	}
159
160
  public static function random($bytes=9){
161
    return strtr(base64_encode(random_bytes($bytes)),'+/=','-_');
162
  }
163
164
}
165