PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/subjects/benchmarks/php-benchmarks/benchcli/tests/test_crypto-md5.php

http://phc.googlecode.com/
PHP | 260 lines | 175 code | 31 blank | 54 comment | 7 complexity | f9fd09dd1d52922973071edff9a6e70f MD5 | raw file
Possible License(s): GPL-2.0, 0BSD, BSD-3-Clause, Unlicense, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. <?php
  2. /*
  3. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  4. * Digest Algorithm, as defined in RFC 1321.
  5. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
  6. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  7. * Distributed under the BSD License
  8. * See http://pajhome.org.uk/crypt/md5 for more info.
  9. */
  10. /*
  11. * Configurable variables. You may need to tweak these to be compatible with
  12. * the server-side, but the defaults work in most cases.
  13. */
  14. $hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  15. $b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  16. $chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  17. /*
  18. * These are the functions you'll usually want to call
  19. * They take string arguments and return either hex or base-64 encoded strings
  20. */
  21. function hex_md5($s)
  22. {
  23. global $chrsz;
  24. return binl2hex(core_md5(str2binl($s), strlen($s) * $chrsz));
  25. }
  26. /**
  27. * Written and contributed by
  28. * Alex Stapleton,
  29. * Andy Doctorow,
  30. * Tarakan,
  31. * Bill Zeller,
  32. * Vijay "Cyberax" Bhatter
  33. * traB
  34. * This code is released into the public domain
  35. * http://www.tapouillo.com/firefox_extension/sourcecode.txt
  36. */
  37. function zeroFill($a, $b)
  38. {
  39. $z = hexdec(80000000);
  40. if ($z & $a)
  41. {
  42. $a = ($a>>1);
  43. $a &= (~$z);
  44. $a |= 0x40000000;
  45. $a = ($a>>($b-1));
  46. }
  47. else
  48. {
  49. $a = ($a>>$b);
  50. }
  51. return $a;
  52. }
  53. /*
  54. * Calculate the MD5 of an array of little-endian words, and a bit length
  55. */
  56. function core_md5($x, $len)
  57. {
  58. /* append padding */
  59. $x[$len >> 5] = NULL;
  60. $x[$len >> 5] |= 0x80 << (($len) % 32);
  61. $x[(zeroFill(($len + 64), 9) << 4) + 14] = $len;
  62. $a = 1732584193;
  63. $b = -271733879;
  64. $c = -1732584194;
  65. $d = 271733878;
  66. for($i = 0; $i < count($x); $i += 16)
  67. {
  68. $olda = $a;
  69. $oldb = $b;
  70. $oldc = $c;
  71. $oldd = $d;
  72. for($j = 0; $j <= 15; $j++)
  73. {
  74. if(!isset($x[$i + $j]))
  75. $x[$i + $j] = NULL;
  76. }
  77. $a = md5_ff($a, $b, $c, $d, $x[$i + 0], 7 , -680876936);
  78. $d = md5_ff($d, $a, $b, $c, $x[$i + 1], 12, -389564586);
  79. $c = md5_ff($c, $d, $a, $b, $x[$i + 2], 17, 606105819);
  80. $b = md5_ff($b, $c, $d, $a, $x[$i + 3], 22, -1044525330);
  81. $a = md5_ff($a, $b, $c, $d, $x[$i + 4], 7 , -176418897);
  82. $d = md5_ff($d, $a, $b, $c, $x[$i + 5], 12, 1200080426);
  83. $c = md5_ff($c, $d, $a, $b, $x[$i + 6], 17, -1473231341);
  84. $b = md5_ff($b, $c, $d, $a, $x[$i + 7], 22, -45705983);
  85. $a = md5_ff($a, $b, $c, $d, $x[$i + 8], 7 , 1770035416);
  86. $d = md5_ff($d, $a, $b, $c, $x[$i + 9], 12, -1958414417);
  87. $c = md5_ff($c, $d, $a, $b, $x[$i + 10], 17, -42063);
  88. $b = md5_ff($b, $c, $d, $a, $x[$i + 11], 22, -1990404162);
  89. $a = md5_ff($a, $b, $c, $d, $x[$i + 12], 7 , 1804603682);
  90. $d = md5_ff($d, $a, $b, $c, $x[$i + 13], 12, -40341101);
  91. $c = md5_ff($c, $d, $a, $b, $x[$i + 14], 17, -1502002290);
  92. $b = md5_ff($b, $c, $d, $a, $x[$i + 15], 22, 1236535329);
  93. $a = md5_gg($a, $b, $c, $d, $x[$i + 1], 5 , -165796510);
  94. $d = md5_gg($d, $a, $b, $c, $x[$i + 6], 9 , -1069501632);
  95. $c = md5_gg($c, $d, $a, $b, $x[$i + 11], 14, 643717713);
  96. $b = md5_gg($b, $c, $d, $a, $x[$i + 0], 20, -373897302);
  97. $a = md5_gg($a, $b, $c, $d, $x[$i + 5], 5 , -701558691);
  98. $d = md5_gg($d, $a, $b, $c, $x[$i + 10], 9 , 38016083);
  99. $c = md5_gg($c, $d, $a, $b, $x[$i + 15], 14, -660478335);
  100. $b = md5_gg($b, $c, $d, $a, $x[$i + 4], 20, -405537848);
  101. $a = md5_gg($a, $b, $c, $d, $x[$i + 9], 5 , 568446438);
  102. $d = md5_gg($d, $a, $b, $c, $x[$i + 14], 9 , -1019803690);
  103. $c = md5_gg($c, $d, $a, $b, $x[$i + 3], 14, -187363961);
  104. $b = md5_gg($b, $c, $d, $a, $x[$i + 8], 20, 1163531501);
  105. $a = md5_gg($a, $b, $c, $d, $x[$i + 13], 5 , -1444681467);
  106. $d = md5_gg($d, $a, $b, $c, $x[$i + 2], 9 , -51403784);
  107. $c = md5_gg($c, $d, $a, $b, $x[$i + 7], 14, 1735328473);
  108. $b = md5_gg($b, $c, $d, $a, $x[$i + 12], 20, -1926607734);
  109. $a = md5_hh($a, $b, $c, $d, $x[$i + 5], 4 , -378558);
  110. $d = md5_hh($d, $a, $b, $c, $x[$i + 8], 11, -2022574463);
  111. $c = md5_hh($c, $d, $a, $b, $x[$i + 11], 16, 1839030562);
  112. $b = md5_hh($b, $c, $d, $a, $x[$i + 14], 23, -35309556);
  113. $a = md5_hh($a, $b, $c, $d, $x[$i + 1], 4 , -1530992060);
  114. $d = md5_hh($d, $a, $b, $c, $x[$i + 4], 11, 1272893353);
  115. $c = md5_hh($c, $d, $a, $b, $x[$i + 7], 16, -155497632);
  116. $b = md5_hh($b, $c, $d, $a, $x[$i + 10], 23, -1094730640);
  117. $a = md5_hh($a, $b, $c, $d, $x[$i + 13], 4 , 681279174);
  118. $d = md5_hh($d, $a, $b, $c, $x[$i + 0], 11, -358537222);
  119. $c = md5_hh($c, $d, $a, $b, $x[$i + 3], 16, -722521979);
  120. $b = md5_hh($b, $c, $d, $a, $x[$i + 6], 23, 76029189);
  121. $a = md5_hh($a, $b, $c, $d, $x[$i + 9], 4 , -640364487);
  122. $d = md5_hh($d, $a, $b, $c, $x[$i + 12], 11, -421815835);
  123. $c = md5_hh($c, $d, $a, $b, $x[$i + 15], 16, 530742520);
  124. $b = md5_hh($b, $c, $d, $a, $x[$i + 2], 23, -995338651);
  125. $a = md5_ii($a, $b, $c, $d, $x[$i + 0], 6 , -198630844);
  126. $d = md5_ii($d, $a, $b, $c, $x[$i + 7], 10, 1126891415);
  127. $c = md5_ii($c, $d, $a, $b, $x[$i + 14], 15, -1416354905);
  128. $b = md5_ii($b, $c, $d, $a, $x[$i + 5], 21, -57434055);
  129. $a = md5_ii($a, $b, $c, $d, $x[$i + 12], 6 , 1700485571);
  130. $d = md5_ii($d, $a, $b, $c, $x[$i + 3], 10, -1894986606);
  131. $c = md5_ii($c, $d, $a, $b, $x[$i + 10], 15, -1051523);
  132. $b = md5_ii($b, $c, $d, $a, $x[$i + 1], 21, -2054922799);
  133. $a = md5_ii($a, $b, $c, $d, $x[$i + 8], 6 , 1873313359);
  134. $d = md5_ii($d, $a, $b, $c, $x[$i + 15], 10, -30611744);
  135. $c = md5_ii($c, $d, $a, $b, $x[$i + 6], 15, -1560198380);
  136. $b = md5_ii($b, $c, $d, $a, $x[$i + 13], 21, 1309151649);
  137. $a = md5_ii($a, $b, $c, $d, $x[$i + 4], 6 , -145523070);
  138. $d = md5_ii($d, $a, $b, $c, $x[$i + 11], 10, -1120210379);
  139. $c = md5_ii($c, $d, $a, $b, $x[$i + 2], 15, 718787259);
  140. $b = md5_ii($b, $c, $d, $a, $x[$i + 9], 21, -343485551);
  141. $a = safe_add($a, $olda);
  142. $b = safe_add($b, $oldb);
  143. $c = safe_add($c, $oldc);
  144. $d = safe_add($d, $oldd);
  145. }
  146. return array($a, $b, $c, $d);
  147. }
  148. /*
  149. * These functions implement the four basic operations the algorithm uses.
  150. */
  151. function md5_cmn($q, $a, $b, $x, $s, $t)
  152. {
  153. return safe_add(bit_rol(safe_add(safe_add($a, $q), safe_add($x, $t)), $s), $b);
  154. }
  155. function md5_ff($a, $b, $c, $d, $x, $s, $t)
  156. {
  157. return md5_cmn(($b & $c) | ((~$b) & $d), $a, $b, $x, $s, $t);
  158. }
  159. function md5_gg($a, $b, $c, $d, $x, $s, $t)
  160. {
  161. return md5_cmn(($b & $d) | ($c & (~$d)), $a, $b, $x, $s, $t);
  162. }
  163. function md5_hh($a, $b, $c, $d, $x, $s, $t)
  164. {
  165. return md5_cmn($b ^ $c ^ $d, $a, $b, $x, $s, $t);
  166. }
  167. function md5_ii($a, $b, $c, $d, $x, $s, $t)
  168. {
  169. return md5_cmn($c ^ ($b | (~$d)), $a, $b, $x, $s, $t);
  170. }
  171. /*
  172. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  173. * to work around bugs in some JS interpreters.
  174. */
  175. function safe_add($x, $y)
  176. {
  177. $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
  178. $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
  179. return ($msw << 16) | ($lsw & 0xFFFF);
  180. }
  181. /*
  182. * Bitwise rotate a 32-bit number to the left.
  183. */
  184. function bit_rol($num, $cnt)
  185. {
  186. return ($num << $cnt) | (zeroFill($num, (32 - $cnt)));
  187. }
  188. /*
  189. * Convert a string to an array of little-endian words
  190. * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
  191. */
  192. function str2binl($str)
  193. {
  194. global $chrsz;
  195. $bin = array();
  196. $mask = (1 << $chrsz) - 1;
  197. for($i = 0; $i < strlen($str) * $chrsz; $i += $chrsz)
  198. {
  199. /* for or setting */
  200. @$bin[$i >> 5] |= (ord($str[$i / $chrsz]) & $mask) << ($i % 32);
  201. }
  202. return $bin;
  203. }
  204. /*
  205. * Convert an array of little-endian words to a hex string.
  206. */
  207. function binl2hex($binarray)
  208. {
  209. global $hexcase;
  210. $hex_tab = $hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  211. $str = "";
  212. for($i = 0; $i < count($binarray) * 4; $i++)
  213. {
  214. $str .= $hex_tab[($binarray[$i >> 2] >> (($i % 4) * 8 + 4)) & 0xF].
  215. $hex_tab[($binarray[$i >> 2] >> (($i % 4) * 8)) & 0xF];
  216. }
  217. return $str;
  218. }
  219. $plainText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ornare pharetra ligula, sed facilisis magna suscipit a. Etiam quis nisi erat, eu feugiat mi. Cras eu odio sem. Proin porttitor lacinia convallis. Duis nec magna massa. Aliquam facilisis, odio eu placerat feugiat, lorem odio accumsan lacus, at fermentum magna ipsum et velit. Vivamus lobortis commodo dolor nec vestibulum. Ut ac diam non sapien sagittis scelerisque. Praesent vitae consequat purus. Suspendisse bibendum varius sapien, quis malesuada lacus mollis bibendum. Sed cursus mattis massa ut pulvinar. Praesent sed felis massa. Duis suscipit ornare purus sit amet posuere. Nullam volutpat nulla a nisi dapibus blandit. Donec at blandit massa. Nam semper, urna nec eleifend faucibus, metus velit semper arcu, at ornare purus sem et diam. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec elementum, lectus eu laoreet congue, justo dolor blandit leo, eget commodo erat eros non augue. ";
  220. for ($i = 0; $i <4; $i++)
  221. {
  222. $plainText .= $plainText;
  223. }
  224. $md5Output = hex_md5($plainText);
  225. /**
  226. * To see results, uncomment these two lines
  227. */
  228. //echo "Actual: ".$md5Output."<br />";
  229. //echo "Expected MD5(): ".md5($plainText)."<br />";