Python模塊-hashlib模塊

hashlib.md5()

通常用於密文存儲和文件完整性對比算法

普通md5編碼轉變安全

# -*- coding:utf8 -*-
import hashlib
obj = hashlib.md5()                     #md5算法
obj.update("hello".encode("utf8"))      #更新字符串轉變md5

print(obj.hexdigest())
5d41402abc4b2a76b9719d911017c592

防撞庫md5編碼轉變編碼

# -*- coding:utf8 -*-
import hashlib
obj = hashlib.md5("new_str".encode("utf8")) #md5算法,加一段本身獨有的字符串
obj.update("hello".encode("utf8"))      #更新字符串轉變md5 
print(obj.hexdigest())
02af9b9b58172b2b44e957b5b18e8c3f

加密疊加,更新的兩次將進行疊加加密

# -*- coding:utf8 -*-
import hashlib
obj = hashlib.md5()                         #md5算法
obj.update("hello".encode("utf8"))          #更新md5編碼1
obj = hashlib.md5("new_str".encode("utf8")) #更新md5編碼2

print(obj.hexdigest())
4796a473717ba01b840e83fd28acf4fd

hashlib.sha256()

用法和md5同樣,在md5的基礎上提高了安全級別,類同的有sha1,sha224,sha384,sha512等code

# -*- coding:utf8 -*-
import hashlib
obj = hashlib.sha256()                  #md5算法
obj.update("hello".encode("utf8"))      #更新字符串轉變sha256
print(obj.hexdigest())
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
相關文章
相關標籤/搜索