hashlib 是一个提供了一些流行的hash算法的 Python 标准库.其中所包括的算法有 md5, sha1, sha224, sha256, sha384, sha512等常用算法
MD5加密算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import hashlib """ MD5加密 """ hash_md5 = hashlib.md5() hash_md5.update(b "hello" ) #b 表示byte类型 hash_md5.update(b "world" ) print (hash_md5.hexdigest()) """ 十六进制MD5加密结果""" print (hash_md5.digest()) """ 二进制MD5加密结果 """ """ 分几次update的参数加密结果和拼接一起加密结果一样""" hashmd5 = hashlib.md5() hashmd5.update(b "helloworld" ) print (hashmd5.hexdigest()) print (hashmd5.digest()) |
运行结果:
1
2
3
4
|
fc5e038d38a57032085441e7fe7010b0 - - 十六进制 b 'xfc^x03x8d8xa5p2x08TAxe7xfepx10xb0' - - 二进制 fc5e038d38a57032085441e7fe7010b0 b 'xfc^x03x8d8xa5p2x08TAxe7xfepx10xb0' |
SHA加密算发,常用256的,位数越长越安全
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
|
import hashlib print ( "hash1加密的二进制和十六进制结果" ) hash1 = hashlib.sha1() hash1.update(b "helloworld" ) print (hash1.digest()) print (hash1.hexdigest()) print ( "hash256加密的二进制和十六进制结果" ) hash256 = hashlib.sha256() hash256.update(b "helloworld" ) print (hash256.digest()) print (hash256.hexdigest()) print ( "hash384加密的二进制和十六进制结果" ) hash384 = hashlib.sha384() hash384.update(b "helloworld" ) print (hash384.digest()) print (hash384.hexdigest()) print ( "hash512加密的二进制和十六进制结果" ) hash512 = hashlib.sha512() hash512.update(b "helloworld" ) print (hash512.digest()) print (hash512.hexdigest()) |
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
|
hash1加密的二进制和十六进制结果 b 'jxdfxb1x83xa4xa2xc9J/x92xdaxb5xadxe7bxa4xx89xa5xa1' 6adfb183a4a2c94a2f92dab5ade762a47889a5a1 hash256加密的二进制和十六进制结果 b 'x93jx18\xaaxa2fxbbx9cxbex98x1ex9ex05xcbxxcds+x0b2x80xebx94Dx12xbbox8fx8fx07xaf' 936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af hash384加密的二进制和十六进制结果 b "x97x98*[x14x14xb9x07x81x03xa1xc0x08xc4xe3Rl'xb4x1cxdbxcfx80yx05`xa4x0f*x9bxf2xedD'xabx14(xx99x15xedK=xc0|EKxd9" 97982a5b1414b9078103a1c008c4e3526c27b41cdbcf80790560a40f2a9bf2ed4427ab1428789915ed4b3dc07c454bd9 hash512加密的二进制和十六进制结果 b 'x15x94$MRxf2xd8xc1+x14+xb6x1fGxbc.xafP=mx9cxa8Hx0cxaex9fxcfx11/fxe4x96}xc5xe8xfax98(^6xdbx8axf1xb8xffxa8xb8Lxb1^x0fxbcxf86xc3xdexb8x03xc1?7ex9a`' 1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60 |
中文的加密,需要编码encode()得到bytes类型,再进行加密操作
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import hashlib print ( "===MD5中文加密举例===" ) china_md5 = hashlib.md5() china_md5.update( "中华人民共和国" .encode(encoding = "utf-8" )) print (china_md5.digest()) print (china_md5.hexdigest()) print ( "===SHA中文加密举例===" ) china_sha = hashlib.sha256() china_sha.update( "中华人民共和国" .encode(encoding = "utf-8" )) print (china_sha.digest()) print (china_sha.hexdigest()) |
运行结果:
1
2
3
4
5
6
|
= = = MD5中文加密举例 = = = b 'x02_xcexabx94x18xbex86x06k`xa7x1bxc7x14x85' 025fceab9418be86066b60a71bc71485 = = = SHA中文加密举例 = = = b 'xcakx1e6x84xd5xaaxeec1x90xb1x82xcexbbx06xd6%xd2x84xfexc8x9ax95x15 -vxf4xd6J?' ca6b1e3684d5aaee633190b182cebb06d625d284fec89a9515202d76f4d64a3f |
hmac 模块,它内部对我们创建 key 和 vlaue 再进行处理然后再加密
散列消息鉴别码,简称HMAC,是一种基于消息鉴别码MAC(Message Authentication Code)的鉴别机制。使用HMAC时,消息通讯的双方,通过验证消息中加入的鉴别密钥K来鉴别消息的真伪;
一般用于网络通信中消息加密,前提是双方先要约定好key,就像接头暗号一样,然后消息发送把用key把消息加密,接收方用key + 消息明文再加密,拿加密后的值 跟 发送者的相对比是否相等,这样就能验证消息的真实性,及发送者的合法性了。
hmac加密示例
1
2
3
4
5
6
7
8
9
10
11
|
import hmac print ( "===hmac字符加密示例===" ) hm = hmac.new(b "hello" ,b "world" ) print ( "hmac二进制加密:" ,hm.digest()) print ( "hmac十六进制加密:" ,hm.hexdigest()) print ( "===hmac中文加密示例===" ) ch = hmac.new( "你好" .encode(encoding = "utf-8" ), "世界" .encode(encoding = "utf-8" )) print ( "hmac二进制加密:" ,ch.digest()) print ( "hmac十六进制加密:" ,ch.hexdigest()) |
运行结果:
1
2
3
4
5
6
|
= = = hmac字符加密示例 = = = hmac二进制加密: b 'x0e%dxb7xe1x00xf044x1exa4wxc2?(;' hmac十六进制加密: 0e2564b7e100f034341ea477c23f283b = = = hmac中文加密示例 = = = hmac二进制加密: b 'xb0x90xbdUxda:xe1xefxa9fts@xbf%
' hmac十六进制加密: b090bd55da3ae1efa966747340bf250a |