PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Eva/Stdlib/String/Hash.php

https://github.com/zc1415926/eva-engine
PHP | 171 lines | 102 code | 25 blank | 44 comment | 15 complexity | 00ce19b9633a62e0b656cc3a782ba348 MD5 | raw file
  1. <?php
  2. /**
  3. * EvaEngine
  4. *
  5. * @link https://github.com/AlloVince/eva-engine
  6. * @copyright Copyright (c) 2012 AlloVince (http://avnpc.com/)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Eva_Api.php
  9. * @author AlloVince
  10. */
  11. namespace Eva\Stdlib\String;
  12. /**
  13. * Unique Hash
  14. * From : http://blog.kevburnsjr.com/php-unique-hash
  15. *
  16. * @category Eva
  17. * @package Eva_Stdlib
  18. */
  19. class Hash
  20. {
  21. public static function guid()
  22. {
  23. if (function_exists('com_create_guid') === true)
  24. {
  25. return trim(com_create_guid(), '{}');
  26. }
  27. return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
  28. }
  29. public static function shortHashArray($input) {
  30. $base32 = array(
  31. "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ,
  32. "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,
  33. "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ,
  34. "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" ,
  35. "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" ,
  36. "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" ,
  37. "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" ,
  38. "U" , "V" , "W" , "X" , "Y" , "Z"
  39. );
  40. $hex = md5($input);
  41. $hexLen = strlen($hex);
  42. $subHexLen = $hexLen / 8;
  43. $output = array();
  44. for ($i = 0; $i < $subHexLen; $i++) {
  45. $subHex = substr ($hex, $i * 8, 8);
  46. $int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
  47. $out = '';
  48. for ($j = 0; $j < 6; $j++) {
  49. $val = 0x0000003D & $int;
  50. $out .= $base32[$val];
  51. $int = $int >> 5;
  52. }
  53. $output[] = $out;
  54. }
  55. return $output;
  56. }
  57. public static function uniqueHash($returnArray = false)
  58. {
  59. $guid = self::guid();
  60. $guid = str_replace('-', '', $guid);
  61. $hashArray = self::shortHashArray($guid);
  62. return false === $returnArray ? $hashArray[rand(0, 3)] : $hashArray;
  63. }
  64. /**
  65. * Translates a number to a short alhanumeric version
  66. *
  67. * Translated any number up to 9007199254740992
  68. * to a shorter version in letters e.g.:
  69. * 9007199254740989 --> PpQXn7COf
  70. * @author Kevin van Zonneveld <kevin@vanzonneveld.net>
  71. * @author Simon Franz
  72. * @author Deadfish
  73. * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  74. * @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
  75. * @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
  76. * @link http://kevin.vanzonneveld.net/
  77. *
  78. * @param mixed $in String or long input to translate
  79. * @param boolean $toNum Reverses translation when true
  80. * @param mixed $padUp Number or boolean padds the result up to a specified length
  81. * @param string $passKey Supplying a password makes it harder to calculate the original ID
  82. *
  83. * @return mixed string or long
  84. */
  85. public static function shortHash($in = null, $toNum = false, $padUp = false, $passKey = null)
  86. {
  87. if(!$in){
  88. $in = mt_rand();
  89. $padUp = 6;
  90. }
  91. $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  92. if ($passKey !== null) {
  93. // Although this function's purpose is to just make the
  94. // ID short - and not so much secure,
  95. // with this patch by Simon Franz (http://blog.snaky.org/)
  96. // you can optionally supply a password to make it harder
  97. // to calculate the corresponding numeric ID
  98. for ($n = 0; $n<strlen($index); $n++) {
  99. $i[] = substr( $index,$n ,1);
  100. }
  101. $passhash = hash('sha256',$passKey);
  102. $passhash = (strlen($passhash) < strlen($index)) ? hash('sha512',$passKey) : $passhash;
  103. for ($n=0; $n < strlen($index); $n++) {
  104. $p[] = substr($passhash, $n ,1);
  105. }
  106. array_multisort($p, SORT_DESC, $i);
  107. $index = implode($i);
  108. }
  109. $base = strlen($index);
  110. if ($toNum) {
  111. // Digital number <<-- alphabet letter code
  112. $in = strrev($in);
  113. $out = 0;
  114. $len = strlen($in) - 1;
  115. for ($t = 0; $t <= $len; $t++) {
  116. $bcpow = bcpow($base, $len - $t);
  117. $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
  118. }
  119. if (is_numeric($padUp)) {
  120. $padUp--;
  121. if ($padUp > 0) {
  122. $out -= pow($base, $padUp);
  123. }
  124. }
  125. $out = sprintf('%F', $out);
  126. $out = substr($out, 0, strpos($out, '.'));
  127. } else {
  128. // Digital number -->> alphabet letter code
  129. if (is_numeric($padUp)) {
  130. $padUp--;
  131. if ($padUp > 0) {
  132. $in += pow($base, $padUp);
  133. }
  134. }
  135. $out = "";
  136. for ($t = floor(log($in, $base)); $t >= 0; $t--) {
  137. $bcp = bcpow($base, $t);
  138. $a = floor($in / $bcp) % $base;
  139. $out = $out . substr($index, $a, 1);
  140. $in = $in - ($a * $bcp);
  141. }
  142. $out = strrev($out); // reverse
  143. }
  144. return $out;
  145. }
  146. }