FNV与FNV-1a Hash算法说明【转】

简介: 转自:http://blog.csdn.net/jiayanhui2877/article/details/12090575 The core of the FNV hash The core of the FNV-1 hash algorithm is as follows: hash ...

转自:http://blog.csdn.net/jiayanhui2877/article/details/12090575

The core of the FNV hash

The core of the FNV-1 hash algorithm is as follows:
hash = offset_basis
for each octet_of_data to be hashed
 hash = hash * FNV_prime
 hash = hash xor octet_of_data
return hash

The offset_basis andFNV_prime can be found in theparameters of the FNV-1/FNV-1a hash section below.

FNV-1a alternate algorithm

There is a minor variation of the FNV hash algorithm known as FNV-1a:
hash = offset_basis
for each octet_of_data to be hashed
 hash = hash xor octet_of_data
 hash = hash * FNV_prime
return hash

The only difference between the FNV-1a hash and the FNV-1 hashis the order of the xor and multiply.The FNV-1a hashuses the same FNV_prime and offset_basisas the FNV-1 hash of the same n-bit size.


Parameters of the FNV-1/FNV-1a hash

The FNV-1 hash parameters are as follows:
  • hash is an n bit unsigned integer,where n is the bit length of hash.
  • The multiplication is performed modulo 2nwhere n is the bit length of hash.
  • The xor is performed on the low orderoctet (8 bits) of hash.
  • The FNV_prime is dependent on n, the size of the hash:
    32 bit FNV_prime =2 24 + 2 8 + 0x93 = 16777619

    64 bit FNV_prime = 2 40 + 2 8 + 0xb3 = 1099511628211

    128 bit FNV_prime = 2 88 + 2 8 + 0x3b = 309485009821345068724781371

    256 bit FNV_prime = 2 168 + 2 8 + 0x63 = 374144419156711147060143317175368453031918731002211

    512 bit FNV_prime = 2 344 + 2 8 + 0x57 =
    35835915874844867368919076489095108449946327955754392558399825615420669938882575
    126094039892345713852759

    1024 bit FNV_prime = 2 680 + 2 8 + 0x8d =
    50164565101131186554345988110352789550307653454047907443030175238311120551081474
    51509157692220295382716162651878526895249385292291816524375083746691371804094271
    873160484737966720260389217684476157468082573

    Part of the magic of FNV is the selection of the FNV_primefor a given sized unsigned integer.Some primes do hash better than other primes for a given integer size.

  • The offset_basis for FNV-1 is dependent on n, the size of the hash:
    32 bit offset_basis = 2166136261

    64 bit offset_basis = 14695981039346656037

    128 bit offset_basis = 144066263297769815596495629667062367629

    256 bit offset_basis =
    100029257958052580907070968620625704837092796014241193945225284501741471925557

    512 bit offset_basis =
    96593031294966694980094354007163104660904187456726378961083743294344626579945829
    32197716438449813051892206539805784495328239340083876191928701583869517785

    1024 bit offset_basis =
    14197795064947621068722070641403218320880622795441933960878474914617582723252296
    73230371772215086409652120235554936562817466910857181476047101507614802975596980
    40773201576924585630032153049571501574036444603635505054127112859663616102678680
    82893823963790439336411086884584107735010676915

    NOTE: Older versions of this web page incorretly indicated that the 128 bitFNV_prime was 2168 + 28 + 0x59.This was not correct.While that satisfied all of the significant FNV_prime properties,it was not the smallest 128 bit FNV_prime.The 128 bit offset_basischanged from275519064689413815358837431229664493455to144066263297769815596495629667062367629was changed as a result of the 128 bit FNV_prime correction.(Sorry about that!)

【作者】 张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
目录
相关文章
|
C语言
[字符串和内存函数]strcmp和strncmp以及memcmp的区别
[字符串和内存函数]strcmp和strncmp以及memcmp的区别
547 0
|
SQL 分布式计算 Java
GraalVM在Facebook大量使用,性能提升显著!
GraalVM在Facebook大量使用,性能提升显著!
932 0
GraalVM在Facebook大量使用,性能提升显著!
|
6月前
|
JSON 前端开发 程序员
告别手动解析!借助 CodeBuddy 快速开发网页源码提取工具
这是一款基于 PyQt5 开发的网页源码解析工具,旨在解决查看网页源代码时功能不足的问题。工具支持加载网页源码、复制源码、解析 JSON 数据和链接、下载页面内容等功能,满足“查看、提取、保存”三大需求。通过不断迭代,增加了格式化 JSON、提取文章与图片链接、保存 HTML 文件等实用功能,为开发者提供高效便捷的源码解析体验。项目已开源,可前往 CNB 查看源码。
|
人工智能 自动驾驶 云栖大会
大模型赋能智能座舱,NVIDIA 深度适配通义千问大模型
9月20日杭州云栖大会上, NVIDIA DRIVE Orin系统级芯片实现了与阿里云通义千问多模态大模型Qwen2-VL的深度适配。阿里云、斑马智行联合NVIDIA英伟达推出舱驾融合大模型解决方案,基于通义大模型开发“能听会看”的智能座舱助理,让车内人员通过语音交流就能操作座舱内的各类应用,享受极致丰富的交互体验。
713 14
|
安全 量子技术 数据安全/隐私保护
量子通信技术的原理与进展
【8月更文挑战第1天】量子通信技术以其独特的优势和巨大的潜力在科技领域掀起了一场革命性的变革。随着研究的深入和技术的成熟,量子通信技术将在未来发挥更加重要的作用,为信息安全、量子计算、量子传感等领域提供强有力的支持。我们有理由相信,在不久的将来,量子通信将以其卓越的性能和广泛的应用前景,为我们带来更加安全、高效、便捷的通信体验。
1281 9
|
存储 监控 关系型数据库
MySQL数据库数据块大小详解
MySQL数据库数据块大小详解
508 0
|
负载均衡 网络协议
虚拟网络技术:bond技术
虚拟网络技术:bond技术
831 0
|
Ubuntu Linux 开发工具
关于【firefly-rk3399】的环境配置以及编译内核遇到的问题,烧写update.img相关量产工具的说明(二)
关于【firefly-rk3399】的环境配置以及编译内核遇到的问题,烧写update.img相关量产工具的说明(二)
446 0
|
数据采集 数据可视化 数据挖掘
Python中如何使用pandas和matplotlib库绘制图表
Python中如何使用pandas和matplotlib库绘制图表
440 0
|
数据采集 Java 调度
深入解析Java线程池的扩容机制与拒绝策略
深入解析Java线程池的扩容机制与拒绝策略
827 0