下载

1下载券

加入VIP
  • 专属下载券
  • 上传内容扩展
  • 资料优先审核
  • 免费资料无限下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 常见hash算法

常见hash算法.doc

常见hash算法

陈建利TED
2012-12-21 0人阅读 举报 0 0 0 暂无简介

简介:本文档为《常见hash算法doc》,可适用于IT/计算机领域

***Hash算法大全<br>*推荐使用FNV算法*algorithmNone*authorGoodzzp*lastEditGoodzzp*editDetailCreate*publicclassHashAlgorithms{***加法hash*paramkey字符串*paramprime一个质数*returnhash结果*publicstaticintadditiveHash(Stringkey,intprime){  inthash,i  for(hash=keylength(),i=i<keylength()i)   hash=keycharAt(i)  return(hashprime)}***旋转hash*paramkey输入字符串*paramprime质数*returnhash值*publicstaticintrotatingHash(Stringkey,intprime){  inthash,i  for(hash=keylength(),i=i<keylength()i)    hash=(hash<<)^(hash>>)^keycharAt(i)  return(hashprime)  return(hash^(hash>>)^(hash>>))}替代:使用:hash=(hash^(hash>>)^(hash>>))mask替代:hash=prime***MASK值随便找一个值最好是质数*staticintMMASK=xfed***一次一个hash*paramkey输入字符串*return输出hash值*publicstaticintoneByOneHash(Stringkey){  int  hash,i  for(hash=,i=i<keylength()i)  {    hash=keycharAt(i)    hash=(hash<<)    hash^=(hash>>)  }  hash=(hash<<)  hash^=(hash>>)  hash=(hash<<)  return(hashMMASK)  returnhash}***Bernstein'shash*paramkey输入字节数组*paramlevel初始hash常量*return结果hash*publicstaticintbernstein(Stringkey){  inthash=  inti  for(i=i<keylength()i)hash=*hashkeycharAt(i)  returnhash}Pearson'sHashcharpearson(charkey,ublen,chartab){  charhash  ubi  for(hash=len,i=i<leni)    hash=tabhash^keyi  return(hash)}CRCHashing计算crc,具体代码见其他ubcrc(char*key,ublen,ubmask,ubtab){  ubhash,i  for(hash=len,i=i<leni)    hash=(hash>>)^tab(hashxff)^keyi  return(hashmask)}***UniversalHashing*publicstaticintuniversal(charkey,intmask,inttab){  inthash=keylength,i,len=keylength  for(i=i<(len<<)i=)  {    chark=keyi>>    if((kx)==)hash^=tabi    if((kx)==)hash^=tabi    if((kx)==)hash^=tabi    if((kx)==)hash^=tabi    if((kx)==)hash^=tabi    if((kx)==)hash^=tabi    if((kx)==)hash^=tabi    if((kx)==)hash^=tabi  }  return(hashmask)}***ZobristHashing*publicstaticintzobrist(charkey,intmask,inttab){  inthash,i  for(hash=keylength,i=i<keylengthi)    hash^=tabikeyi  return(hashmask)}LOOKUP见BobJenkins()c文件位FNV算法staticintMSHIFT=***位的FNV算法*paramdata数组*returnint值*   publicstaticintFNVHash(bytedata)   {       inthash=(int)L       for(byteb:data)           hash=(hash*)^b       if(MSHIFT==)           returnhash       return(hash^(hash>>MSHIFT))MMASK   }   **    *改进的位FNV算法    *paramdata数组    *returnint值    *   publicstaticintFNVHash(bytedata)   {       finalintp=       inthash=(int)L       for(byteb:data)           hash=(hash^b)*p       hash=hash<<       hash^=hash>>       hash=hash<<       hash^=hash>>       hash=hash<<       returnhash   }   **    *改进的位FNV算法    *paramdata字符串    *returnint值    *   publicstaticintFNVHash(Stringdata)   {       finalintp=       inthash=(int)L       for(inti=i<datalength()i)           hash=(hash^datacharAt(i))*p       hash=hash<<       hash^=hash>>       hash=hash<<       hash^=hash>>       hash=hash<<       returnhash   }   **    *ThomasWang的算法整数hash    *   publicstaticintintHash(intkey)   {     key=~(key<<)     key^=(key>>>)     key=(key<<)     key^=(key>>>)     key=~(key<<)     key^=(key>>>)     returnkey   }   **    *RS算法hash    *paramstr字符串    *   publicstaticintRSHash(Stringstr)   {       intb   =       inta   =       inthash=      for(inti=i<strlength()i)      {         hash=hash*astrcharAt(i)         a   =a*b      }      return(hashxFFFFFFF)   }   *EndOfRSHashFunction*   **    *JS算法    *   publicstaticintJSHash(Stringstr)   {      inthash=      for(inti=i<strlength()i)      {         hash^=((hash<<)strcharAt(i)(hash>>))      }      return(hashxFFFFFFF)   }   *EndOfJSHashFunction*   **    *PJW算法    *   publicstaticintPJWHash(Stringstr)   {       intBitsInUnsignedInt=       intThreeQuarters    =(BitsInUnsignedInt*)       intOneEighth        =BitsInUnsignedInt       intHighBits         =xFFFFFFFF<<(BitsInUnsignedIntOneEighth)       inthash             =       inttest             =      for(inti=i<strlength()i)      {         hash=(hash<<OneEighth)strcharAt(i)         if((test=hashHighBits)!=)         {            hash=((hash^(test>>ThreeQuarters))(~HighBits))         }      }      return(hashxFFFFFFF)   }   *EndOfPJWeinbergerHashFunction*   **    *ELF算法    *   publicstaticintELFHash(Stringstr)   {       inthash=       intx   =      for(inti=i<strlength()i)      {         hash=(hash<<)strcharAt(i)         if((x=(int)(hashxFL))!=)         {            hash^=(x>>)            hash=~x         }      }      return(hashxFFFFFFF)   }   *EndOfELFHashFunction*   **    *BKDR算法    *   publicstaticintBKDRHash(Stringstr)   {       intseed=etc       inthash=      for(inti=i<strlength()i)      {         hash=(hash*seed)strcharAt(i)      }      return(hashxFFFFFFF)   }   *EndOfBKDRHashFunction*   **    *SDBM算法    *   publicstaticintSDBMHash(Stringstr)   {       inthash=      for(inti=i<strlength()i)      {         hash=strcharAt(i)(hash<<)(hash<<)hash      }      return(hashxFFFFFFF)   }   *EndOfSDBMHashFunction*   **    *DJB算法    *   publicstaticintDJBHash(Stringstr)   {      inthash=      for(inti=i<strlength()i)      {         hash=((hash<<)hash)strcharAt(i)      }      return(hashxFFFFFFF)   }   *EndOfDJBHashFunction*   **    *DEK算法    *   publicstaticintDEKHash(Stringstr)   {       inthash=strlength()      for(inti=i<strlength()i)      {         hash=((hash<<)^(hash>>))^strcharAt(i)      }      return(hashxFFFFFFF)   }   *EndOfDEKHashFunction*   **    *AP算法    *   publicstaticintAPHash(Stringstr)   {       inthash=      for(inti=i<strlength()i)      {         hash^=((i)==)((hash<<)^strcharAt(i)^(hash>>)):                                  (~((hash<<)^strcharAt(i)^(hash>>)))      }      return(hashxFFFFFFF)      returnhash   }   *EndOfAPHashFunction*      **    *JAVA自己带的算法    *   publicstaticintjava(Stringstr){  inth=  intoff=  intlen=strlength()  for(inti=i<leni)  {   h=*hstrcharAt(off)  }  returnh}      **    *混合hash算法输出位的值    *   publicstaticlongmixHash(Stringstr)   {   longhash=strhashCode()   hash<<=   hash|=FNVHash(str)   returnhash   }

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

评分:

/9

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利