File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # hmac
2+
3+ HMAC 是用于消息认证的加密哈希算法,全称是 keyed-Hash Message Authentication Code。** HMAC 利用哈希算法,以一个密钥和一个消息作为输入,生成一个加密串作为输出** 。HMAC 可以有效防止类似 MD5 的彩虹表等攻击,比如将常见密码的 MD5 值存入数据库,可能被反向破解。
4+
5+ Python 的 ` hmac ` 模块提供了 HMAC 算法,它的使用形式是:
6+
7+ ``` python
8+ hmac.new(key[, msg[, digestmod]])
9+ ```
10+
11+ 其中,key 是一个密钥;msg 是消息,可选,如果给出 msg,则调用方法 ` update(msg) ` ;digestmod 是 HMAC 对象使用的摘要构造函数或模块,默认为 ` hashlib.md5 ` 构造函数。
12+
13+ HMAC 对象常用的方法有:
14+
15+ - HMAC.update(msg)
16+
17+ 用字符串 msg 更新 HMAC 对象,重复的调用等同于一次调用所有参数的组合,即:
18+
19+ ```
20+ m.update(a);
21+ m.update(b);
22+ ```
23+
24+ 相当于
25+
26+ ```
27+ m.update(a+b)
28+ ```
29+
30+ - HMAC.digest()
31+
32+ 返回目前传递给 ` update() ` 方法的字符串的摘要。此字符串长度将与给构造函数的摘要的 digest_size 相同。它可能包含非 ASCII 字符,包括 NULL 字节。
33+
34+ - HMAC.hexdigest()
35+
36+ 类似 digest(),但是返回的摘要的字符串的长度翻倍,且只包含十六进制数字。
37+
38+ 现在,让我们看一个简单的例子:
39+
40+ ``` python
41+ >> > from datetime import datetime
42+ >> > import hashlib
43+ >> > import hmac
44+
45+ >> > key = ' you-never-know'
46+ >> > msg = datetime.utcnow().strftime(' %Y-%m-%d ' )
47+
48+ >> > m = hmac.new(key, msg, hashlib.sha1)
49+ >> > signature = m.hexdigest()
50+ >> > signature
51+ ' fdb2087a66a2f00afbc1884738467ba089782779'
52+ ```
53+
54+ # 参考资料
55+
56+ - [ hmac — 用于消息认证的加密哈希算法] ( http://python.usyiyi.cn/python_278/library/hmac.html )
57+
58+
You can’t perform that action at this time.
0 commit comments