Completed
Push — master ( 99713d...638416 )
by Stefano
03:28
created
classes/Hash.php 3 patches
Indentation   +98 added lines, -98 removed lines patch added patch discarded remove patch
@@ -11,104 +11,104 @@  discard block
 block discarded – undo
11 11
  */
12 12
 
13 13
 class Hash {
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() {
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 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) {
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 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
-		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) {
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
-		case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
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',
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
-	}
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
+    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) {
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
+    case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
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',
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 112
 
113 113
   public static function murmur($key, $seed = 0, $as_integer=false) {
114 114
     $key  = array_values(unpack('C*',(string) $key));
@@ -145,8 +145,8 @@  discard block
 block discarded – undo
145 145
     $h1  = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
146 146
     $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000);
147 147
 
148
-		return $as_integer ? $h1 : base_convert($h1 ,10, 32);
149
-	}
148
+    return $as_integer ? $h1 : base_convert($h1 ,10, 32);
149
+  }
150 150
 
151 151
   public static function random($bytes=9){
152 152
     return strtr(base64_encode(static::random_bytes($bytes)),'+/=','-_');
Please login to merge, or discard this patch.
Switch Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -71,42 +71,42 @@
 block discarded – undo
71 71
 
72 72
 	public static function uuid($type = 4, $namespace = '', $name = '') {
73 73
 		switch ($type) {
74
-		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) {
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
-		case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
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',
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));
74
+		  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) {
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
+		  case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
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',
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 110
 		}
111 111
 	}
112 112
 
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
 	 */
44 44
 	public static function methods() {
45 45
     // Merge PHP provided algos with ours (murmur)
46
-		return array_merge(hash_algos(), ['murmur','murmurhash3']);
46
+		return array_merge(hash_algos(), ['murmur', 'murmurhash3']);
47 47
 	}
48 48
 
49 49
 	/**
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
 	 */
58 58
 	public static function can($algo) {
59 59
     // Faster than : in_array(explode(',',implode(',',static::methods())))
60
-		return strpos(implode(',',static::methods()).',', "$algo,") !== false;
60
+		return strpos(implode(',', static::methods()).',', "$algo,") !== false;
61 61
 	}
62 62
 
63 63
 	/**
@@ -71,33 +71,33 @@  discard block
 block discarded – undo
71 71
 
72 72
 	public static function uuid($type = 4, $namespace = '', $name = '') {
73 73
 		switch ($type) {
74
-		case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
74
+		case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
75 75
 				'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) {
76 76
 				return false;
77 77
 			}
78 78
 
79 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]));
80
+			$nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) {
81
+				$nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1]));
82 82
 			}
83 83
 
84
-			$hash = md5($nstr . $name);
84
+			$hash = md5($nstr.$name);
85 85
 			return sprintf('%08s-%04s-%04x-%04x-%12s',
86 86
 				substr($hash, 0, 8), substr($hash, 8, 4),
87 87
 				(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
88 88
 				(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
89 89
 				substr($hash, 20, 12));
90
-		case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
90
+		case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
91 91
 				'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) {
92 92
 				return false;
93 93
 			}
94 94
 
95 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]));
96
+			$nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) {
97
+				$nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1]));
98 98
 			}
99 99
 
100
-			$hash = sha1($nstr . $name);
100
+			$hash = sha1($nstr.$name);
101 101
 			return sprintf('%08s-%04s-%04x-%04x-%12s',
102 102
 				substr($hash, 0, 8), substr($hash, 8, 4),
103 103
 				(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
@@ -110,11 +110,11 @@  discard block
 block discarded – undo
110 110
 		}
111 111
 	}
112 112
 
113
-  public static function murmur($key, $seed = 0, $as_integer=false) {
114
-    $key  = array_values(unpack('C*',(string) $key));
113
+  public static function murmur($key, $seed = 0, $as_integer = false) {
114
+    $key  = array_values(unpack('C*', (string) $key));
115 115
     $klen = count($key);
116
-    $h1   = (int)$seed;
117
-    for ($i=0,$bytes=$klen-($remainder=$klen&3) ; $i<$bytes ; ) {
116
+    $h1   = (int) $seed;
117
+    for ($i = 0, $bytes = $klen - ($remainder = $klen & 3);$i < $bytes;) {
118 118
       $k1 = $key[$i]
119 119
         | ($key[++$i] << 8)
120 120
         | ($key[++$i] << 16)
@@ -145,14 +145,14 @@  discard block
 block discarded – undo
145 145
     $h1  = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
146 146
     $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000);
147 147
 
148
-		return $as_integer ? $h1 : base_convert($h1 ,10, 32);
148
+		return $as_integer ? $h1 : base_convert($h1, 10, 32);
149 149
 	}
150 150
 
151
-  public static function random($bytes=9){
152
-    return strtr(base64_encode(static::random_bytes($bytes)),'+/=','-_');
151
+  public static function random($bytes = 9) {
152
+    return strtr(base64_encode(static::random_bytes($bytes)), '+/=', '-_');
153 153
   }
154 154
 
155
-  public static function random_bytes($bytes){
155
+  public static function random_bytes($bytes) {
156 156
     static $randf = null;
157 157
     if (function_exists('random_bytes')) {
158 158
       return \random_bytes($bytes);
Please login to merge, or discard this patch.