python md5

import hashlib
#obj = hashlib.md5(bytes('',encoding='utf-8'))
obj=hashlib.md5(bytes('123',encoding='utf-8'))
result = obj.hexdigest()
print(result)python

hashlib

用於加密相關的操做,代替了md5模塊和sha模塊,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法算法

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
import hashlib
 
# ######## md5 ########
hash = hashlib.md5()
# help(hash.update)
hash .update(bytes( 'admin' , encoding = 'utf-8' ))
print ( hash .hexdigest())
print ( hash .digest())
 
 
######## sha1 ########
 
hash = hashlib.sha1()
hash .update(bytes( 'admin' , encoding = 'utf-8' ))
print ( hash .hexdigest())
 
# ######## sha256 ########
 
hash = hashlib.sha256()
hash .update(bytes( 'admin' , encoding = 'utf-8' ))
print ( hash .hexdigest())
 
 
# ######## sha384 ########
 
hash = hashlib.sha384()
hash .update(bytes( 'admin' , encoding = 'utf-8' ))
print ( hash .hexdigest())
 
# ######## sha512 ########
 
hash = hashlib.sha512()
hash .update(bytes( 'admin' , encoding = 'utf-8' ))
print ( hash .hexdigest())

以上加密算法雖然依然很是厲害,但時候存在缺陷,即:經過撞庫能夠反解。因此,有必要對加密算法中添加自定義key再來作加密。ide

1
2
3
4
5
6
7
import hashlib
 
# ######## md5 ########
 
hash = hashlib.md5(bytes( '898oaFs09f' ,encoding = "utf-8" ))
hash .update(bytes( 'admin' ,encoding = "utf-8" ))
print ( hash .hexdigest())

python內置還有一個 hmac 模塊,它內部對咱們建立 key 和 內容 進行進一步的處理而後再加密加密

1
2
3
4
5
import hmac
 
h = hmac.new(bytes( '898oaFs09f' ,encoding = "utf-8" ))
h.update(bytes( 'admin' ,encoding = "utf-8" ))
print (h.hexdigest())
相關文章
相關標籤/搜索