Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. <?php
  2.  
  3. const B16_STR = '0123456789abcdef';
  4. const B36_STR = '0123456789abcdefghijklmnopqrstuvwxyz';
  5. const B64_STR = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
  6.  
  7. function gcd($a,$b){
  8. return ($a%$b)?gcd($b,$a%$b):$b;
  9. }
  10. function convert($str, $from, $to){
  11. $len_from = strlen($from);
  12. $len_to = strlen($to);
  13. if(!preg_match('/^['.$from.']+$/'.(preg_match('/^([^[:upper:]]*|[^[:lower:]]*)(?1)*(?!\1)$/', $from)?'':'i'),$str)
  14. || $len_from<=0 || $len_to<=0){
  15. throw new Exception();
  16. }
  17. $buf = [];
  18. $offset = 0;
  19. $l = strlen($str);
  20. $c = 0;
  21. $bits_from;$bits_to;
  22. $segs_from;$segs_to;
  23. {
  24. $log2 = log(2);
  25. $bits_from = log($len_from)/$log2;
  26. $bits_to = log($len_to)/$log2;
  27. $lcm = $bits_from*$bits_to/gcd($bits_from,$bits_to);
  28. $segs_from = $lcm / $bits_from;
  29. $segs_to = $lcm / $bits_to;
  30. }
  31. while($offset<$l){
  32. for($j=0;$j<$segs_from;$j++){
  33. $c |= strpos($from, $str[($offset<$l)?$offset++:0])<<($bits_from*$j);
  34. }
  35. for($i=0;$i<$segs_to;$i++){
  36. array_push($buf, $to[($c>>($i*$bits_to))&($len_to-1)]);
  37. }
  38. $c = 0;
  39. }
  40. return implode($buf);
  41. }
  42. function hex64($str){
  43. return convert($str, B16_STR, B64_STR);
  44. }
  45. function hex64r($str){
  46. return convert($str, B64_STR, B16_STR);
  47. }
  48. function convertTest($TEST_COUNT = 1000000){
  49. $failed = 0;
  50. for($i=0;$i<$TEST_COUNT;$i++){
  51. $h = hash('whirlpool', 'a'.rand());
  52. $t = substr(hex64r(hex64($h)),0,strlen($h));
  53. if(!$h===$t){
  54. echo $h,"\n<br>",$t,"\n<br>\n<br>";
  55. $failed++;
  56. }
  57. }
  58. echo 'Passed ',$TEST_COUNT-$failed,' out of ',$TEST_COUNT;
  59. }
  60.  
  61. convertTest(1000);
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement