Hashids.php 8.3 KB

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