3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class EAHashor { private $right32; //initialise the class public function __construct() { // This is a 32- and 64-bit safe way to get an integer with only the // right-most 32-bits set $this->right32 = ~((~0 << 16) << 16); } public function eaEncode($string){ $a = "67452301"; $b = "EFCDAB89"; $c = "98BADCFE"; $d = "10325476"; $words = $this->init($string); for($i = 0, $l = count($words) / 16 - 1; $i <= $l; $i++){ $A = $a; $B = $b; $C = $c; $D = $d; /* ROUND 1 */ $this->FF ($A, $B, $C, $D, $words[0 + ($i * 16)], 7, "d76aa478"); $this->FF ($D, $A, $B, $C, $words[1 + ($i * 16)], 12, "e8c7b756"); $this->FF ($C, $D, $A, $B, $words[2 + ($i * 16)], 17, "242070db"); $this->FF ($B, $C, $D, $A, $words[3 + ($i * 16)], 22, "c1bdceee"); $this->FF ($A, $B, $C, $D, $words[4 + ($i * 16)], 7, "f57c0faf"); $this->FF ($D, $A, $B, $C, $words[5 + ($i * 16)], 12, "4787c62a"); $this->FF ($C, $D, $A, $B, $words[6 + ($i * 16)], 17, "a8304613"); $this->FF ($B, $C, $D, $A, $words[7 + ($i * 16)], 22, "fd469501"); $this->FF ($A, $B, $C, $D, $words[8 + ($i * 16)], 7, "698098d8"); $this->FF ($D, $A, $B, $C, $words[9 + ($i * 16)], 12, "8b44f7af"); $this->FF ($C, $D, $A, $B, $words[10 + ($i * 16)], 17, "ffff5bb1"); $this->FF ($B, $C, $D, $A, $words[11 + ($i * 16)], 22, "895cd7be"); $this->FF ($A, $B, $C, $D, $words[12 + ($i * 16)], 7, "6b901122"); $this->FF ($D, $A, $B, $C, $words[13 + ($i * 16)], 12, "fd987193"); $this->FF ($C, $D, $A, $B, $words[14 + ($i * 16)], 17, "a679438e"); $this->FF ($B, $C, $D, $A, $words[15 + ($i * 16)], 22, "49b40821"); /* ROUND 2 */ $this->GG ($A, $B, $C, $D, $words[1 + ($i * 16)], 5, "f61e2562"); $this->GG ($D, $A, $B, $C, $words[6 + ($i * 16)], 9, "c040b340"); $this->GG ($C, $D, $A, $B, $words[11 + ($i * 16)], 14, "265e5a51"); $this->GG ($B, $C, $D, $A, $words[0 + ($i * 16)], 20, "e9b6c7aa"); $this->GG ($A, $B, $C, $D, $words[5 + ($i * 16)], 5, "d62f105d"); $this->GG ($D, $A, $B, $C, $words[10 + ($i * 16)], 9, "02441453"); $this->GG ($C, $D, $A, $B, $words[15 + ($i * 16)], 14, "d8a1e681"); $this->GG ($B, $C, $D, $A, $words[4 + ($i * 16)], 20, "e7d3fbc8"); $this->GG ($A, $B, $C, $D, $words[9 + ($i * 16)], 5, "21e1cde6"); $this->GG ($D, $A, $B, $C, $words[14 + ($i * 16)], 9, "c33707d6"); $this->GG ($C, $D, $A, $B, $words[3 + ($i * 16)], 14, "f4d50d87"); $this->GG ($B, $C, $D, $A, $words[8 + ($i * 16)], 20, "455a14ed"); $this->GG ($A, $B, $C, $D, $words[13 + ($i * 16)], 5, "a9e3e905"); $this->GG ($D, $A, $B, $C, $words[2 + ($i * 16)], 9, "fcefa3f8"); $this->GG ($C, $D, $A, $B, $words[7 + ($i * 16)], 14, "676f02d9"); $this->GG ($B, $C, $D, $A, $words[12 + ($i * 16)], 20, "8d2a4c8a"); /* ROUND 3 */ $this->HH ($A, $B, $C, $D, $words[5 + ($i * 16)], 4, "fffa3942"); $this->HH ($D, $A, $B, $C, $words[8 + ($i * 16)], 11, "8771f681"); //HH ($C, $D, $A, $B, $words[11 + ($i * 16)], 16, "6d9d6122"); //the line below has been changed to use 14 rather than 16 as seen in the commented line $this->HH ($C, $D, $A, $B, $words[11 + ($i * 16)], 14, "6d9d6122"); $this->HH ($B, $C, $D, $A, $words[14 + ($i * 16)], 23, "fde5380c"); $this->HH ($A, $B, $C, $D, $words[1 + ($i * 16)], 4, "a4beea44"); $this->HH ($D, $A, $B, $C, $words[4 + ($i * 16)], 11, "4bdecfa9"); $this->HH ($C, $D, $A, $B, $words[7 + ($i * 16)], 16, "f6bb4b60"); $this->HH ($B, $C, $D, $A, $words[10 + ($i * 16)], 23, "bebfbc70"); $this->HH ($A, $B, $C, $D, $words[13 + ($i * 16)], 4, "289b7ec6"); $this->HH ($D, $A, $B, $C, $words[0 + ($i * 16)], 11, "eaa127fa"); $this->HH ($C, $D, $A, $B, $words[3 + ($i * 16)], 16, "d4ef3085"); $this->HH ($B, $C, $D, $A, $words[6 + ($i * 16)], 23, "04881d05"); $this->HH ($A, $B, $C, $D, $words[9 + ($i * 16)], 4, "d9d4d039"); $this->HH ($D, $A, $B, $C, $words[12 + ($i * 16)], 11, "e6db99e5"); $this->HH ($C, $D, $A, $B, $words[15 + ($i * 16)], 16, "1fa27cf8"); $this->HH ($B, $C, $D, $A, $words[2 + ($i * 16)], 23, "c4ac5665"); /* ROUND 4 */ $this->II ($A, $B, $C, $D, $words[0 + ($i * 16)], 6, "f4292244"); $this->II ($D, $A, $B, $C, $words[7 + ($i * 16)], 10, "432aff97"); $this->II ($C, $D, $A, $B, $words[14 + ($i * 16)], 15, "ab9423a7"); $this->II ($B, $C, $D, $A, $words[5 + ($i * 16)], 21, "fc93a039"); $this->II ($A, $B, $C, $D, $words[12 + ($i * 16)], 6, "655b59c3"); $this->II ($D, $A, $B, $C, $words[3 + ($i * 16)], 10, "8f0ccc92"); $this->II ($C, $D, $A, $B, $words[10 + ($i * 16)], 15, "ffeff47d"); $this->II ($B, $C, $D, $A, $words[1 + ($i * 16)], 21, "85845dd1"); $this->II ($A, $B, $C, $D, $words[8 + ($i * 16)], 6, "6fa87e4f"); $this->II ($D, $A, $B, $C, $words[15 + ($i * 16)], 10, "fe2ce6e0"); $this->II ($C, $D, $A, $B, $words[6 + ($i * 16)], 15, "a3014314"); $this->II ($B, $C, $D, $A, $words[13 + ($i * 16)], 21, "4e0811a1"); $this->II ($A, $B, $C, $D, $words[4 + ($i * 16)], 6, "f7537e82"); $this->II ($D, $A, $B, $C, $words[11 + ($i * 16)], 10, "bd3af235"); $this->II ($C, $D, $A, $B, $words[2 + ($i * 16)], 15, "2ad7d2bb"); $this->II ($B, $C, $D, $A, $words[9 + ($i * 16)], 21, "eb86d391"); //this last line is used twice $this->II ($B, $C, $D, $A, $words[9 + ($i * 16)], 21, "eb86d391"); $this->addVars($a, $b, $c, $d, $A, $B, $C, $D); } $MD5 = ''; foreach (array($a, $b, $c, $d) as $x) { $MD5 .= implode('', array_reverse(str_split($this->leftpad($x, 8), 2))); } return $MD5; } /* General functions */ private function hexbin($str){ $hexbinmap = array( "0" => "0000", "1" => "0001", "2" => "0010", "3" => "0011" , "4" => "0100" , "5" => "0101" , "6" => "0110" , "7" => "0111" , "8" => "1000" , "9" => "1001" , "A" => "1010" , "a" => "1010" , "B" => "1011" , "b" => "1011" , "C" => "1100" , "c" => "1100" , "D" => "1101" , "d" => "1101" , "E" => "1110" , "e" => "1110" , "F" => "1111" , "f" => "1111"); $bin = ""; for ($i = 0; $i < strlen($str); $i++){ $bin .= $hexbinmap[$str[$i]]; } $bin = ltrim($bin, '0'); // echo "Original: ".$str." New: ".$bin."<br />"; return $bin; } private function strhex($str){ $hex = ""; for ($i = 0; $i < strlen($str); $i++){ $hex = $hex.$this->leftpad(dechex(ord($str[$i])), 2); } return $hex; } /* MD5-specific functions */ private function init($string){ $len = strlen($string) * 8; $hex = $this->strhex($string); // convert ascii string to hex $bin = $this->leftpad($this->hexbin($hex), $len); // convert hex string to bin $padded = $this->pad($bin); $padded = $this->pad($padded, 1, $len); $block = str_split($padded, 32); foreach ($block as &$b) { $b = implode('', array_reverse(str_split($b, 8))); } return $block; } private function pad($bin, $type=0, $len = 0){ if($type == 0){ $bin = $bin."1"; $buff = strlen($bin) % 512; if($buff != 448){ while(strlen($bin) % 512 != 448){ $bin = $bin."0"; } } } // append length (b) of string to latter 64 bits elseif($type == 1){ $bLen = $this->leftpad(decbin($len), 64); $bin .= implode('', array_reverse(str_split($bLen, 8))); } return $bin; } /* MD5 base functions */ private function F($X, $Y, $Z){ $X = $this->binarySafeHexDec($X); $Y = $this->binarysafehexdec($Y); $Z = $this->binarysafehexdec($Z); $calc = (($X & $Y) | ((~ $X) & $Z)); // X AND Y OR NOT X AND Z return $calc; } private function G($X, $Y, $Z){ $X = $this->binarysafehexdec($X); $Y = $this->binarysafehexdec($Y); $Z = $this->binarysafehexdec($Z); $calc = (($X & $Z) | ($Y & (~ $Z))); // X AND Z OR Y AND NOT Z return $calc; } private function H($X, $Y, $Z){ $X = $this->binarysafehexdec($X); $Y = $this->binarysafehexdec($Y); $Z = $this->binarysafehexdec($Z); $calc = ($X ^ $Y ^ $Z); // X XOR Y XOR Z return $calc; } private function I($X, $Y, $Z){ $X = $this->binarysafehexdec($X); $Y = $this->binarysafehexdec($Y); $Z = $this->binarysafehexdec($Z); $calc = ($Y ^ ($X | (~ $Z))) ; // Y XOR (X OR NOT Z) return $calc; } /* MD5 round functions */ /* $A - hex, $B - hex, $C - hex, $D - hex (F - dec) $M - binary $s - decimal $t - hex */ private function FF(&$A, $B, $C, $D, $M, $s, $t){ $A = $this->binarysafehexdec($A); $t = $this->binarysafehexdec($t); $M = $this->binarysafebindec($M); $A = $this->add($A, $this->F($B, $C, $D), $M, $t) & $this->right32; //decimal $A = $this->rotate($A, $s); $A = dechex($this->add($this->binarysafehexdec($B), $this->binarysafehexdec($A)) & $this->right32); } private function GG(&$A, $B, $C, $D, $M, $s, $t){ $A = $this->binarysafehexdec($A); $t = $this->binarysafehexdec($t); $M = $this->binarysafebindec($M); $A = $this->add($A, $this->G($B, $C, $D), $M, $t) & $this->right32; //decimal $A = $this->rotate($A, $s); $A = dechex($this->add($this->binarysafehexdec($B), $this->binarysafehexdec($A)) & $this->right32); } private function HH(&$A, $B, $C, $D, $M, $s, $t){ $A = $this->binarysafehexdec($A); $t = $this->binarysafehexdec($t); $M = $this->binarysafebindec($M); $A = $this->add($A, $this->H($B, $C, $D), $M, $t) & $this->right32; //decimal $A = $this->rotate($A, $s); $A = dechex($this->add($this->binarysafehexdec($B), $this->binarysafehexdec($A)) & $this->right32); } private function II(&$A, $B, $C, $D, $M, $s, $t){ $A = $this->binarysafehexdec($A); $t = $this->binarysafehexdec($t); $M = $this->binarysafebindec($M); $A = $this->add($A, $this->I($B, $C, $D), $M, $t) & $this->right32; //decimal $A = $this->rotate($A, $s); $A = dechex($this->add($this->binarysafehexdec($B), $this->binarysafehexdec($A)) & $this->right32); } // shift private function rotate ($decimal, $bits) { //returns hex printf("Rotate: %b (%d)\nResult: %b\n", $decimal, $bits, (($decimal << $bits) | ($decimal >> (32 - $bits))) & $this->right32); return dechex((($decimal << $bits) | ($decimal >> (32 - $bits))) & $this->right32); } private function addVars(&$a, &$b, &$c, &$d, $A, $B, $C, $D){ $A = $this->binarysafehexdec($A); $B = $this->binarysafehexdec($B); $C = $this->binarysafehexdec($C); $D = $this->binarysafehexdec($D); $aa = $this->binarysafehexdec($a); $bb = $this->binarysafehexdec($b); $cc = $this->binarysafehexdec($c); $dd = $this->binarysafehexdec($d); $aa = $this->add($aa, $A) & $this->right32; $bb = $this->add($bb, $B) & $this->right32; $cc = $this->add($cc, $C) & $this->right32; $dd = $this->add($dd, $D) & $this->right32; $a = dechex($aa); $b = dechex($bb); $c = dechex($cc); $d = dechex($dd); } private function leftpad($needs_padding, $alignment) { if (strlen($needs_padding) % $alignment) { $pad_amount = $alignment - strlen($needs_padding) % $alignment; $left_pad = implode('', array_fill(0, $pad_amount, '0')); $needs_padding = $left_pad . $needs_padding; } return $needs_padding; } public function binarySafeBinDec($bin) { $bits = array_reverse(str_split($bin, 1)); $result = 0; foreach ($bits as $position => $bit) { $result |= ((int) $bit) << $position; } return $result; } private function binarySafeHexDec($hex) { $h = str_split(str_pad($hex, 8, '0', STR_PAD_LEFT), 2); return (hexdec($h[0]) << 24) | (hexdec($h[1]) << 16) | (hexdec($h[2]) << 8) | hexdec($h[3]); } private function leftShiftByOne($intAsStr) { $p = unpack('N2', $intAsStr); return pack('N2', ($p[1] << 1) | (($p[2] >> 32) & 0x00000001), $p[2] << 1); } // This is not a true binary addition function. In the context in which it is used // it works, but it's not a general case routine. private function binarySafeAddition($a, $b) { // NB: we don't actually need 64 bits, but 40 bit integers are confusing $a = "\x00\x00\x00\x00" . pack('N', $a); $b = "\x00\x00\x00\x00" . pack('N', $b); $carry = $a & $b; $result = $a ^ $b; while ($carry != "\x00\x00\x00\x00\x00\x00\x00\x00") { $shiftedcarry = $this->leftShiftByOne($carry); $carry = $result & $shiftedcarry; $result ^= $shiftedcarry; } return current(unpack('N', substr($result, 4))); } public function add() { $result = 0; foreach (func_get_args() as $int) { $result = $this->binarySafeAddition($result, $int); } return $result; } } $eahashor = new EAHashor(); $hash = $eahashor->eaEncode('test'); echo $hash;
Output for 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Rotate: 1001011110111100000100111101011 (7) Result: 11101111000001001111010110100101 Rotate: 11000111111000110000101000100100 (12) Result: 110000101000100100110001111110 Rotate: 10101011101101001111000100000110 (17) Result: 11100010000011010101011101101001 Rotate: 11000000110111000101111110100101 (22) Result: 11101001011100000011011100010111 Rotate: 10101001110101010111011001100001 (7) Result: 11101010101110110011000011010100 Rotate: 1000111100111110001111111101011 (12) Result: 11110001111111101011010001111001 Rotate: 1100111101100001100100000101100 (17) Result: 10010000010110001100111101100001 Rotate: 1011101111001011011111010000101 (22) Result: 10100001010101110111100101101111 Rotate: 10001101110100101010011101010000 (7) Result: 11101001010100111010100001000110 Rotate: 1011000001100111101110000001 (12) Result: 1100111101110000001000010110000 Rotate: 11011001110010101011011011010 (17) Result: 10101101101101000011011001110010 Rotate: 10101101011000011100010101000110 (22) Result: 1010001101010110101100001110001 Rotate: 1101000011011001111010011101110 (7) Result: 110110011110100111011100110100 Rotate: 11110000010111111100010101110101 (12) Result: 11111100010101110101111100000101 Rotate: 11111111110111000101011111010001 (17) Result: 10101111101000111111111110111000 Rotate: 11101111110101111110001110011010 (22) Result: 11100110101110111111010111111000 Rotate: 1110111000111100001011101000011 (5) Result: 11100011110000101110100001101110 Rotate: 110011110101110011000110110100 (9) Result: 10101110011000110110100001100111 Rotate: 100110110000000011000011010100 (14) Result: 1100001101010000100110110000 Rotate: 10010001101111111001110111001 (20) Result: 111011100100010010001101111111 Rotate: 1110001011000001101001011100110 (5) Result: 101100000110100101110011001110 Rotate: 11110101000001100101111111010010 (9) Result: 1100101111111010010111101010 Rotate: 10001111101101111011101111101000 (14) Result: 11101110111110100010001111101101 Rotate: 11011001011110100101111001101101 (20) Result: 11100110110111011001011110100101 Rotate: 101100100111001110000100110100 (5) Result: 10010011100111000010011010000101 Rotate: 11001000001011001111111110011010 (9) Result: 1011001111111110011010110010000 Rotate: 11010110110000101100001100001001 (14) Result: 10110000110000100111010110110000 Rotate: 10111010001010010110010110000100 (20) Result: 1011000010010111010001010010110 Rotate: 11101001110111011101100000010111 (5) Result: 111011101110110000001011111101 Rotate: 1000010000101110101110100010110 (9) Result: 101110101110100010110010000100 Rotate: 1001111001000001110111000101100 (14) Result: 111011100010110001001111001000 Rotate: 10110111101011100111101011110011 (20) Result: 10101111001110110111101011100111 Rotate: 10001100000001010111000011111 (4) Result: 11000000010101110000111110001 Rotate: 11000100110111010010100010000010 (11) Result: 11101001010001000001011000100110 Rotate: 110110000110000001100111101001 (14) Result: 110011110100100110110000110 Rotate: 1111101011001001011110110111111 (23) Result: 11011111101111101011001001011110 Rotate: 1100110101100110110000111110 (4) Result: 11001101011001101100001111100000 Rotate: 1010000110100111011001110000101 (11) Result: 10011101100111000010101010000110 Rotate: 10101111011010001001110100000110 (16) Result: 10011101000001101010111101101000 Rotate: 11111001100100001101010100110 (23) Result: 1010011000011111001100100001101 Rotate: 11101100000111111111000111010111 (4) Result: 11000001111111110001110101111110 Rotate: 1001010001010101000000011100010 (11) Result: 1010100000001110001001001010001 Rotate: 11010010001001011001110000111101 (16) Result: 10011100001111011101001000100101 Rotate: 11111111000000100010011111011110 (23) Result: 11101111011111111000000100010011 Rotate: 10111101100110010101001001010111 (4) Result: 11011001100101010010010101111011 Rotate: 111001111101001010110011000001 (11) Result: 10100101011001100000100111001111 Rotate: 11000000010100001010111110011100 (16) Result: 10101111100111001100000001010000 Rotate: 110011000011111100110010010 (23) Result: 11001001000000110011000011111100 Rotate: 10011011000110101011110110100000 (6) Result: 11000110101011110110100000100110 Rotate: 11001010110110101001011001001100 (10) Result: 1101010010110010011001100101011 Rotate: 101100101110011110111111110111 (15) Result: 11110111111110111001011001011100 Rotate: 10001001011001101100001010111110 (21) Result: 1010111110100010010110011011000 Rotate: 11000011100000110000111000011011 (6) Result: 11100000110000111000011011110000 Rotate: 110110110000000001101001011101 (10) Result: 11010010111010011011011 Rotate: 1000111010110110100010011101100 (15) Result: 10100010011101100010001110101101 Rotate: 100010101101110001011111101 (21) Result: 1011111101000001000101011011100 Rotate: 11001101001000100011011001000111 (6) Result: 1001000100011011001000111110011 Rotate: 10011011010011101001100100010 (10) Result: 10100111010011001000100001001101 Rotate: 11101010010100010010111001000100 (15) Result: 10010111001000100111010100101000 Rotate: 1011110010011001110111101111101 (21) Result: 11101111101010111100100110011101 Rotate: 10110111101001001000001011001100 (6) Result: 11101001001000001011001100101101 Rotate: 1011101111011000011000111110111 (10) Result: 10110000110001111101110101110111 Rotate: 1111000001000110101011000001100 (15) Result: 10101011000001100011110000010001 Rotate: 10001101010100000101001011101100 (21) Result: 1011101100100011010101000001010 Rotate: 101111110100001100100110101011 (21) Result: 110101011001011111101000011001 6337a43e8cd36058e80ae8cb4f465998