FNV哈希算法
作者:互联网
FNV哈希算法
FNV版本
FNV哈希分为3个版本:fnv-0(已废弃),FNV-1,FNV-1a
算法实现
FNV-0算法公式
hash = 0 for each byte_of_data to be hashed hash = hash * FNV_prime hash = hash ^ octet_of_data return hash
FNV-1算法公式
hash = FNV_offset_basis for each byte_of_data to be hashed hash = hash * FNV_prime hash = hash ^ byte_of_data return hash
FNV-1a算法公式
hash = FNV_offset_basis for each byte_of_data to be hashed hash = hash ^ byte_of_data hash = hash * FNV_prime return hash
FNV公式参数说明
1. FNV_offset_basis
这个参数的值是固定的,根据不同的位数选择不同的值作为hash的首次填充值
<table> <thead> <tr class="header"> <th>位数</th> <th>十进制值</th> </tr> </thead> <tbody> <tr class="odd"> <td>32</td> <td>2166136261</td> </tr> <tr class="even"> <td>64</td> <td>14695981039346656037</td> </tr> <tr class="odd"> <td>128</td> <td>144066263297769815596495629667062367629</td> </tr> <tr class="even"> <td>256</td> <td>100029257958052580907070968620625704837092796014241193945225284501741471925557</td> </tr> <tr class="odd"> <td>512</td> <td>965930312949666949800943540071631046609041874567263789610837432943446265799458<br/> 2932197716438449813051892206539805784495328239340083876191928701583869517785</td> </tr> <tr class="even"> <td>1024</td> <td>1419779506494762106872207064140321832088062279544193396087847491461758272325<br/> 2296732303717722150864096521202355549365628174669108571814760471015076148029<br/> 7559698040773201576924585630032153049571501574036444603635505054127112859663<br/> 61610267868082893823963790439336411086884584107735010676915</td> </tr> </tbody> </table>
2. FNV_prime
还没有看懂,不过这不影响我们实现通用32位,64位的FNV算法
<table> <thead> <tr class="header"> <th>位数</th> <th>十进制值</th> </tr> </thead> <tbody> <tr class="odd"> <td>32</td> <td>16777619</td> </tr> <tr class="even"> <td>64</td> <td>1099511628211</td> </tr> <tr class="odd"> <td>128</td> <td>309485009821345068724781371</td> </tr> <tr class="even"> <td>256</td> <td>374144419156711147060143317175368453031918731002211</td> </tr> <tr class="odd"> <td>512</td> <td>35835915874844867368919076489095108449946327955754392558399825615420669938882<br/> 575126094039892345713852759</td> </tr> <tr class="even"> <td>1024</td> <td>501645651011311865543459881103527895503076534540479074430301752383111205510814745<br/> 150915769222029538271616265187852689524938529229181652437508374669137180409427187<br/> 3160484737966720260389217684476157468082573 </td> </tr> </tbody> </table>
3. hash
hash后的最总结果
4. byte_of_data
8位无符号整数
5. hashed
要加密的数据,需要转换成byte_of_data[]然后循环^计算
标签:FNV,hash,hashed,算法,哈希,byte,data 来源: https://blog.51cto.com/u_7605937/2703502