脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python hashlib和hmac模块使用方法解析

Python hashlib和hmac模块使用方法解析

2021-08-11 00:15Zombie☠️ Python

这篇文章主要介绍了Python hashlib和hmac模块使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

python之hashlib模块:主要提供字符加密功能,python3中将md5和sha模块整合到了hashlib模块,支持md5,sha1, sha224, sha256, sha384, sha512等算法

?
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
import hashlib
# md5 加密算法
a = hashlib.md5()
a.update("Hello Lanten.".encode("utf-8"))
print("md5 加密算法:", a.hexdigest())
 
# sha224 加密算法
b = hashlib.sha224()
b.update("Hello Lanten.".encode("utf-8"))
print("sha224 加密算法:", b.hexdigest())
 
# sha256 加密算法
c = hashlib.sha256()
c.update("Hello Lanten.".encode("utf-8"))
print("sha256 加密算法:", c.hexdigest())
 
# sha384 加密算法
d = hashlib.sha384()
d.update("Hello Lanten.".encode("utf-8"))
print("sha384 加密算法:", d.hexdigest())
 
# sha512 加密算法
e = hashlib.sha512()
e.update("Hello Lanten.".encode("utf-8"))
print("sha512 加密算法:", e.hexdigest())

python之hmac模块:可以对我们创建的key和内容进行处理后再进行加密

?
1
2
3
4
5
6
7
8
# hmac 加密算法模块
import hmac
message = b"Hello Lanten."
key = b"secret"
h = hmac.new(key, message, digestmod = "MD5")
# h = hmac.new(key)
# h.update(message)
print("hmac 加密算法:", h.hexdigest())

输出结果:

Python hashlib和hmac模块使用方法解析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/lanten2020/p/13851121.html

延伸 · 阅读

精彩推荐