[PHP] PHP哈希算法设计 →→→→→进入此内容的聊天室

来自 , 2019-09-09, 写在 PHP, 查看 119 次.
URL http://www.code666.cn/view/a87d27f7
  1. static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength)
  2. {
  3.     register ulong hash = 5381;
  4.  
  5.     /* variant with the hash unrolled eight times */
  6.         for (; nKeyLength >= 8; nKeyLength -= 8) {
  7.         hash = ((hash << 5) + hash) + *arKey++;
  8.         hash = ((hash << 5) + hash) + *arKey++;
  9.         hash = ((hash << 5) + hash) + *arKey++;
  10.         hash = ((hash << 5) + hash) + *arKey++;
  11.         hash = ((hash << 5) + hash) + *arKey++;
  12.         hash = ((hash << 5) + hash) + *arKey++;
  13.         hash = ((hash << 5) + hash) + *arKey++;
  14.         hash = ((hash << 5) + hash) + *arKey++;
  15.     }
  16.     switch (nKeyLength) {
  17.         case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
  18.         case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
  19.         case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
  20.         case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
  21.         case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
  22.         case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
  23.         case 1: hash = ((hash << 5) + hash) + *arKey++; break;
  24.         case 0: break;
  25. EMPTY_SWITCH_DEFAULT_CASE()
  26.     }
  27.     return hash;
  28. }
  29.  
  30.  
  31.   hashing function used in Perl 5.005:
  32.   # Return the hashed value of a string: $hash = perlhash("key")
  33.  # (Defined by the PERL_HASH macro in hv.h)
  34.  sub perlhash
  35.   {
  36.       $hash = 0;
  37.       foreach (split //, shift) {
  38.           $hash = $hash*33 + ord($_);
  39.       }
  40.       return $hash;
  41.   }
  42.  
  43.  

回复 "PHP哈希算法设计"

这儿你可以回复上面这条便签

captcha