Hashids.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. /*
  15. Hashids
  16. http://hashids.org/php
  17. (c) 2013 Ivan Akimov
  18. https://github.com/ivanakimov/hashids.php
  19. hashids may be freely distributed under the MIT license.
  20. */
  21. namespace Lib;
  22. class Hashids {
  23. const VERSION = '1.0.5';
  24. /* internal settings */
  25. const MIN_ALPHABET_LENGTH = 16;
  26. const SEP_DIV = 3.5;
  27. const GUARD_DIV = 12;
  28. /* error messages */
  29. const E_ALPHABET_LENGTH = 'alphabet must contain at least %d unique characters';
  30. const E_ALPHABET_SPACE = 'alphabet cannot contain spaces';
  31. /* set at constructor */
  32. private $_alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890';
  33. private $_seps = 'cfhistuCFHISTU';
  34. private $_min_hash_length = 0;
  35. private $_math_functions = array();
  36. private $_max_int_value = 1000000000;
  37. public function __construct($salt = '', $min_hash_length = 0, $alphabet = '') {
  38. /* if either math precision library is present, raise $this->_max_int_value */
  39. if (function_exists('gmp_add')) {
  40. $this->_math_functions['add'] = 'gmp_add';
  41. $this->_math_functions['div'] = 'gmp_div';
  42. $this->_math_functions['str'] = 'gmp_strval';
  43. } else if (function_exists('bcadd')) {
  44. $this->_math_functions['add'] = 'bcadd';
  45. $this->_math_functions['div'] = 'bcdiv';
  46. $this->_math_functions['str'] = 'strval';
  47. }
  48. $this->_lower_max_int_value = $this->_max_int_value;
  49. if ($this->_math_functions) {
  50. $this->_max_int_value = PHP_INT_MAX;
  51. }
  52. /* handle parameters */
  53. $this->_salt = $salt;
  54. if ((int)$min_hash_length > 0) {
  55. $this->_min_hash_length = (int)$min_hash_length;
  56. }
  57. if ($alphabet) {
  58. $this->_alphabet = implode('', array_unique(str_split($alphabet)));
  59. }
  60. if (strlen($this->_alphabet) < self::MIN_ALPHABET_LENGTH) {
  61. throw new \Exception(sprintf(self::E_ALPHABET_LENGTH, self::MIN_ALPHABET_LENGTH));
  62. }
  63. if (is_int(strpos($this->_alphabet, ' '))) {
  64. throw new \Exception(self::E_ALPHABET_SPACE);
  65. }
  66. $alphabet_array = str_split($this->_alphabet);
  67. $seps_array = str_split($this->_seps);
  68. $this->_seps = implode('', array_intersect($alphabet_array, $seps_array));
  69. $this->_alphabet = implode('', array_diff($alphabet_array, $seps_array));
  70. $this->_seps = $this->_consistent_shuffle($this->_seps, $this->_salt);
  71. if (!$this->_seps || (strlen($this->_alphabet) / strlen($this->_seps)) > self::SEP_DIV) {
  72. $seps_length = (int)ceil(strlen($this->_alphabet) / self::SEP_DIV);
  73. if ($seps_length == 1) {
  74. $seps_length++;
  75. }
  76. if ($seps_length > strlen($this->_seps)) {
  77. $diff = $seps_length - strlen($this->_seps);
  78. $this->_seps .= substr($this->_alphabet, 0, $diff);
  79. $this->_alphabet = substr($this->_alphabet, $diff);
  80. } else {
  81. $this->_seps = substr($this->_seps, 0, $seps_length);
  82. }
  83. }
  84. $this->_alphabet = $this->_consistent_shuffle($this->_alphabet, $this->_salt);
  85. $guard_count = (int)ceil(strlen($this->_alphabet) / self::GUARD_DIV);
  86. if (strlen($this->_alphabet) < 3) {
  87. $this->_guards = substr($this->_seps, 0, $guard_count);
  88. $this->_seps = substr($this->_seps, $guard_count);
  89. } else {
  90. $this->_guards = substr($this->_alphabet, 0, $guard_count);
  91. $this->_alphabet = substr($this->_alphabet, $guard_count);
  92. }
  93. }
  94. public function encode() {
  95. $ret = '';
  96. $numbers = func_get_args();
  97. if (func_num_args() == 1 && is_array(func_get_arg(0))) {
  98. $numbers = $numbers[0];
  99. }
  100. if (!$numbers) {
  101. return $ret;
  102. }
  103. foreach ($numbers as $number) {
  104. $is_number = ctype_digit((string)$number);
  105. if (!$is_number || $number < 0 || $number > $this->_max_int_value) {
  106. return $ret;
  107. }
  108. }
  109. return $this->_encode($numbers);
  110. }
  111. public function decode($hash) {
  112. $ret = array();
  113. if (!$hash || !is_string($hash) || !trim($hash)) {
  114. return $ret;
  115. }
  116. return $this->_decode(trim($hash), $this->_alphabet);
  117. }
  118. public function encode_hex($str) {
  119. if (!ctype_xdigit((string)$str)) {
  120. return '';
  121. }
  122. $numbers = trim(chunk_split($str, 12, ' '));
  123. $numbers = explode(' ', $numbers);
  124. foreach ($numbers as $i => $number) {
  125. $numbers[$i] = hexdec('1' . $number);
  126. }
  127. return call_user_func_array(array($this, 'encode'), $numbers);
  128. }
  129. public function decode_hex($hash) {
  130. $ret = "";
  131. $numbers = $this->decode($hash);
  132. foreach ($numbers as $i => $number) {
  133. $ret .= substr(dechex($number), 1);
  134. }
  135. return $ret;
  136. }
  137. public function get_max_int_value() {
  138. return $this->_max_int_value;
  139. }
  140. private function _encode(array $numbers) {
  141. $alphabet = $this->_alphabet;
  142. $numbers_size = sizeof($numbers);
  143. $numbers_hash_int = 0;
  144. foreach ($numbers as $i => $number) {
  145. $numbers_hash_int += ($number % ($i + 100));
  146. }
  147. $lottery = $ret = $alphabet[$numbers_hash_int % strlen($alphabet)];
  148. foreach ($numbers as $i => $number) {
  149. $alphabet = $this->_consistent_shuffle($alphabet, substr($lottery . $this->_salt . $alphabet, 0, strlen($alphabet)));
  150. $ret .= $last = $this->_hash($number, $alphabet);
  151. if ($i + 1 < $numbers_size) {
  152. $number %= (ord($last) + $i);
  153. $seps_index = $number % strlen($this->_seps);
  154. $ret .= $this->_seps[$seps_index];
  155. }
  156. }
  157. if (strlen($ret) < $this->_min_hash_length) {
  158. $guard_index = ($numbers_hash_int + ord($ret[0])) % strlen($this->_guards);
  159. $guard = $this->_guards[$guard_index];
  160. $ret = $guard . $ret;
  161. if (strlen($ret) < $this->_min_hash_length) {
  162. $guard_index = ($numbers_hash_int + ord($ret[2])) % strlen($this->_guards);
  163. $guard = $this->_guards[$guard_index];
  164. $ret .= $guard;
  165. }
  166. }
  167. $half_length = (int)(strlen($alphabet) / 2);
  168. while (strlen($ret) < $this->_min_hash_length) {
  169. $alphabet = $this->_consistent_shuffle($alphabet, $alphabet);
  170. $ret = substr($alphabet, $half_length) . $ret . substr($alphabet, 0, $half_length);
  171. $excess = strlen($ret) - $this->_min_hash_length;
  172. if ($excess > 0) {
  173. $ret = substr($ret, $excess / 2, $this->_min_hash_length);
  174. }
  175. }
  176. return $ret;
  177. }
  178. private function _decode($hash, $alphabet) {
  179. $ret = array();
  180. $hash_breakdown = str_replace(str_split($this->_guards), ' ', $hash);
  181. $hash_array = explode(' ', $hash_breakdown);
  182. $i = 0;
  183. if (sizeof($hash_array) == 3 || sizeof($hash_array) == 2) {
  184. $i = 1;
  185. }
  186. $hash_breakdown = $hash_array[$i];
  187. if (isset($hash_breakdown[0])) {
  188. $lottery = $hash_breakdown[0];
  189. $hash_breakdown = substr($hash_breakdown, 1);
  190. $hash_breakdown = str_replace(str_split($this->_seps), ' ', $hash_breakdown);
  191. $hash_array = explode(' ', $hash_breakdown);
  192. foreach ($hash_array as $sub_hash) {
  193. $alphabet = $this->_consistent_shuffle($alphabet, substr($lottery . $this->_salt . $alphabet, 0, strlen($alphabet)));
  194. $ret[] = (int)$this->_unhash($sub_hash, $alphabet);
  195. }
  196. if ($this->_encode($ret) != $hash) {
  197. $ret = array();
  198. }
  199. }
  200. return $ret;
  201. }
  202. private function _consistent_shuffle($alphabet, $salt) {
  203. if (!strlen($salt)) {
  204. return $alphabet;
  205. }
  206. for ($i = strlen($alphabet) - 1, $v = 0, $p = 0; $i > 0; $i--, $v++) {
  207. $v %= strlen($salt);
  208. $p += $int = ord($salt[$v]);
  209. $j = ($int + $v + $p) % $i;
  210. $temp = $alphabet[$j];
  211. $alphabet[$j] = $alphabet[$i];
  212. $alphabet[$i] = $temp;
  213. }
  214. return $alphabet;
  215. }
  216. private function _hash($input, $alphabet) {
  217. $hash = '';
  218. $alphabet_length = strlen($alphabet);
  219. do {
  220. $hash = $alphabet[$input % $alphabet_length] . $hash;
  221. if ($input > $this->_lower_max_int_value && $this->_math_functions) {
  222. $input = $this->_math_functions['str']($this->_math_functions['div']($input, $alphabet_length));
  223. } else {
  224. $input = (int)($input / $alphabet_length);
  225. }
  226. } while ($input);
  227. return $hash;
  228. }
  229. private function _unhash($input, $alphabet) {
  230. $number = 0;
  231. if (strlen($input) && $alphabet) {
  232. $alphabet_length = strlen($alphabet);
  233. $input_chars = str_split($input);
  234. foreach ($input_chars as $i => $char) {
  235. $pos = strpos($alphabet, $char);
  236. if ($this->_math_functions) {
  237. $number = $this->_math_functions['str']($this->_math_functions['add']($number, $pos * pow($alphabet_length, (strlen($input) - $i - 1))));
  238. } else {
  239. $number += $pos * pow($alphabet_length, (strlen($input) - $i - 1));
  240. }
  241. }
  242. }
  243. return $number;
  244. }
  245. }