2 error_reporting(E_ALL | E_STRICT);
7 * @author Roland Haeder <roland@mxchange.org>
8 * @copyright Copyright (c) 2013 by Core Developer Team
9 * @license See LICENSE (public-domain)
13 * Calculates a simple but stronger hash from given string. No salts are being
16 * @param $str The string to be hashed
17 * @return $hash The hash from string $str
19 function hashString ($str) {
20 // Calculate strong hash from given string
21 $hash = mhash(MHASH_RIPEMD320, $str);
23 // Return it hexadecimal-encoded
24 return bin2hex($hash);
28 * Double-hashes given string. This is done by hashing the given string and
29 * then hashing the generated hash again.
31 * @param $str The string to be hashed 4 times
32 * @return $hash The generated hash
34 function doubleHashString ($str) {
35 // Generate hash from given hash
36 $hash = hashString(hashString($str));
43 * Calculates a "modula-hash" based given two hashes.
45 * @param $hash1 Hash 1
46 * @param $hash2 Hash 2
48 function modulaHash ($hash1, $hash2) {
49 // Both must have same length
50 assert(strlen($hash1) === strlen($hash2));
55 // "Walk" trough first hash and get every 2 byte of both hashes
56 for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
60 // Get both hash parts and convert to ASCII number
61 $part1 = hexdec(substr($hash1, $idx, 2));
62 $part2 = hexdec(substr($hash2, $idx, 2));
65 * If part1 is larget part2, part1 is divident and vise-versa. But don't do it
68 if (($part1 > $part2) && ($part2 > 0)) {
69 // 'part1' is larger than 'part2'
70 $mod = $part1 % $part2;
71 } elseif (($part1 < $part2) && ($part1 > 0)) {
72 // 'part2' is larger than 'part1'
73 $mod = $part2 % $part1;
76 // "Invert" the result against 255
79 // Encode to hex, pre-pad it with zeros and add to new hash
80 $modulaHash .= padHex($mod);
83 // Modula hash must have same length as input hash
84 assert(strlen($modulaHash) === strlen($hash1));
91 * Calculates a "sqrt-hash" based given two hashes and single-hash it
93 * @param $hash1 Hash 1
94 * @param $hash2 Hash 2
96 function sqrtHash ($hash1, $hash2) {
97 // Both must have same length
98 assert(strlen($hash1) === strlen($hash2));
103 // "Walk" trough first hash and get every 2 byte of both hashes
104 for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
108 // Get both hash parts and convert to ASCII number
109 $part1 = hexdec(substr($hash1, $idx, 2));
110 $part2 = hexdec(substr($hash2, $idx, 2));
112 // Calculate square root of both parts being multiplied and round up, then "invert" it against 255
113 $sqrt = intval(255 - ceil(sqrt($part1 * $part2)));
115 // Encode to hex, pre-pad it with zeros and add to new hash
116 $sqrtHash .= padHex($sqrt);
119 // "sqrt-hash" must have same length as input hash
120 assert(strlen($sqrtHash) === strlen($hash1));
122 // Hash reversed "sqrt-hash" again and return it
123 return hashString(strrev($sqrtHash));
127 * Converts a number between 0 and 255 into a zero-padded hexadecimal string
129 * @param $num Number between 0 and 255
130 * @return $hex Hexadecimal string, padded with zeros
132 function padHex ($num) {
133 // Must be a integer number and between 0 and 255
134 assert(is_int($num));
139 $hex = str_pad(dechex($num), 2, '0', STR_PAD_LEFT);
146 * Calculate "genesis" hashes, please note that these "genesis strings" are now
147 * known to the public as you can read them here in source code and therefore I
148 * will not use them for the real genesis hashes.
151 // A famous quote from Deus Ex 2 - Invisible War
152 doublehashString('"Informations must be free." - AI Helios'),
153 // My name + URL of my first StatusNet instance
154 doubleHashString('Roland Haeder, http://status.mxchange.org'),
155 // A famous quote from Linus Torwalds
156 doubleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'),
157 // Possible truth ;-)
158 doubleHashString('September 11 is a big lie.'),
161 doubleHashString('GNU is Not Uni*.'),
162 // WINE is not an emulator
163 doubleHashString('WINE Is Not an Emulator.'),
164 // FlightGear - Fly free!
165 doubleHashString('FlightGear - Fly free!'),
166 // Linus Torwalds Quote
167 doubleHashString('Your code is shit.. your argument is shit.'),
170 // Calculate "modula hash" from 1st/4th and 2nd/3rd
171 $modulaHashes = array(
173 modulaHash($hashes[0], $hashes[3]),
174 modulaHash($hashes[1], $hashes[2]),
177 modulaHash($hashes[4], $hashes[7]),
178 modulaHash($hashes[5], $hashes[6]),
181 // Calculate "sqrt hash"
183 sqrtHash($modulaHashes[0], $modulaHashes[1]),
184 sqrtHash($modulaHashes[2], $modulaHashes[3])
187 // Calulcate modula hash
188 $modulaHash = modulaHash($sqrtHashes[0], $sqrtHashes[1]);
191 print ('hashes=' . print_r($hashes, TRUE));
192 print ('modulaHashes=' . print_r($modulaHashes, TRUE));
193 print ('sqrtHashes=' . print_r($sqrtHashes, TRUE));
194 print ('modulaHash=' . $modulaHash . PHP_EOL);