Time33哈希算法

字符串哈希函数,发现几乎所有的流行的HashMap都采用了DJB Hash Function,俗称“Times33”算法。Times33的算法很简单,就是不断的乘33,见下面算法原型。

hash(i) = hash(i-1) * 33 + str[i]

 

uint32_t time33(char const *str, int len) 
    { 
        unsigned long  hash = 0; 
        for (int i = 0; i < len; i++) { 
            hash = hash *33 + (unsigned long) str[i]; 
        } 
        return hash; 
    }
 
什么是33的倍数, 
PHP中注释是

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
  This is Daniel J. Bernstein's popular `times 33' hash function as
  posted by him years ago on comp.lang.c. It basically uses a function
  like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best
  known hash functions for strings. Because it is both computed very
  fast and distributes very well.
  The magic of number 33, i.e. why it works better than many other
  constants, prime or not, has never been adequately explained by
  anyone. So I try an explanation: if one experimentally tests all
  multipliers between 1 and 256 (as RSE did now) one detects that even
  numbers are not useable at all. The remaining 128 odd numbers
  (except for the number 1) work more or less all equally well. They
  all distribute in an acceptable way and this way fill a hash table
  with an average percent of approx. 86%.
  If one compares the Chi^2 values of the variants, the number 33 not
  even has the best value. But the number 33 and a few other equally
  good numbers like 17, 31, 63, 127 and 129 have nevertheless a great
  advantage to the remaining numbers in the large set of possible
  multipliers: their multiply operation can be replaced by a faster
  operation based on just one shift plus either a single addition
  or subtraction operation. And because a hash function has to both
  distribute good _and_ has to be very fast to compute, those few
  numbers should be preferred and seems to be the reason why Daniel J.
  Bernstein also preferred it.
                   -- Ralf S. Engelschall rse@engelschall.com
 
 
### 各种哈希算法的性能对比分析 #### 1. **效率** 在计算速度上,MD5 是最快的哈希算法之一,因为它设计简单且运算复杂度较低。然而,这种高效率是以牺牲安全性为代价的。SHA-1 的运行时间稍慢于 MD5,但仍保持较高的处理速度。相比之下,SHA-256 的计算开销显著增加,这使得它的执行时间明显更长[^1]。 #### 2. **安全性** 从安全角度来看,SHA-256 提供了最高的防护水平,能够有效抵御当前已知的各种攻击手段。而 MD5 和 SHA-1 则由于被发现存在严重的碰撞漏洞,已经不再适合用于需要高度安全保障的应用环境[^3]。具体来说,MD5 和 SHA-1 已经可以轻松通过特定方法找到两个不同的输入产生相同的输出结果(即发生碰撞),这对依赖这些算法来验证数据完整性的系统构成了巨大威胁[^2]。 #### 3. **应用领域** 尽管 MD5 因其快速特性仍可能存在于某些低风险环境中作为简单的校验工具;但对于涉及敏感信息保护的任务,则强烈推荐采用像 SHA-256 这样具备更强抗碰撞性能的新一代标准[^4]。此外,在实际部署过程中还需要综合考量硬件资源消耗等因素决定最适合的具体实现方案。 ```python import hashlib import time def measure_hash_performance(data, algorithm): start_time = time.time() hash_func = getattr(hashlib, algorithm)() hash_func.update(data.encode('utf-8')) end_time = time.time() return (end_time - start_time) data_sample = "This is a test string to compare the performance of different hashing algorithms." md5_time = measure_hash_performance(data_sample, 'md5') sha1_time = measure_hash_performance(data_sample, 'sha1') sha256_time = measure_hash_performance(data_sample, 'sha256') print(f"Time taken by MD5: {md5_time}") print(f"Time taken by SHA1: {sha1_time}") print(f"Time taken by SHA256: {sha256_time}") ``` 上述代码片段展示了如何测量不同哈希函数的时间表现差异。通过这种方式可以帮助开发者直观了解各自的优势所在并据此做出合理的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值