Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
djc-github
explore-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
patch-1
Breadcrumbs
explore-python
/
Standard-Modules
/
hmac.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
58 lines (36 loc) · 1.67 KB
patch-1
Breadcrumbs
explore-python
/
Standard-Modules
/
hmac.md
Copy path
Top
File metadata and controls
Code
Blame
58 lines (36 loc) · 1.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# hmac
HMAC
是用于消息认证的加密哈希算法
,
全称是
keyed
-
Hash
Message
Authentication
Code
。
*
*
HMAC
利用哈希算法
,
以一个密钥和一个消息作为输入
,
生成一个加密串作为输出
**
。
HMAC
可以有效防止类似
MD5
的彩虹表等攻击
,
比如将常见密码的
MD5
值存入数据库
,
可能被反向破解
。
Python
的
`hmac`
模块提供了
HMAC
算法
,
它的使用形式是
:
``
`
python
hmac
.
new
(
key
[,
msg
[,
digestmod
]])
``
`
其中
,
key
是一个密钥
;
msg
是消息
,
可选
,
如果给出
msg
,
则调用方法
`update(msg)`
;
digestmod
是
HMAC
对象使用的摘要构造函数或模块
,
默认为
`hashlib.md5`
构造函数
。
HMAC
对象常用的方法有
:
-
HMAC
.
update
(
msg
)
用字符串
msg
更新
HMAC
对象
,
重复的调用等同于一次调用所有参数的组合
,
即
:
``
`
m
.
update
(
a
);
m
.
update
(
b
);
``
`
相当于
``
`
m
.
update
(
a
+
b
)
``
`
-
HMAC
.
digest
()
返回目前传递给
`update()`
方法的字符串的摘要
。
此字符串长度将与给构造函数的摘要的
digest_size
相同
。
它可能包含非
ASCII
字符
,
包括
NULL
字节
。
-
HMAC
.
hexdigest
()
类似
digest
(),
但是返回的摘要的字符串的长度翻倍
,
且只包含十六进制数字
。
现在
,
让我们看一个简单的例子
:
``
`
python
>>
>
from
datetime
import
datetime
>
>>
import
hashlib
>
>>
import
hmac
>
>>
key
=
'you-never-know'
>
>>
msg
=
datetime
.
utcnow
().
strftime
(
'%Y-%m-%d'
)
>
>>
m
=
hmac
.
new
(
key
,
msg
,
hashlib
.
sha1
)
>
>>
signature
=
m
.
hexdigest
()
>
>>
signature
'fdb2087a66a2f00afbc1884738467ba089782779'
``
`
# 参考资料
-
[
hmac
—
用于消息认证的加密哈希算法
](
http
:
//
python
.
usyiyi
.
cn
/
python_278
/
library
/
hmac
.
html
)
You can’t perform that action at this time.