Python中hashlib模块的使用

hashlib是 python 下一款与加密相关的库包,提供摘要算法:md5、sha1、sha224、sha256、sha384、sha512、blake2b、blake2s、sha3_224、sha3_256、sha3_384、sha3_512、shake_128、shake_256。摘要算法通过摘要函数(单向函数)对任意长度的数据计算出固定长度的摘要,目的是为了验证原始数据是否被篡改。

MD5函数的使用,其他函数类似

import hashlib
#方法一
m=hashlib.md5()
m.update(b"root")      #提供的字符需要字节类型的
print(m.digest)        #返回一个btye类型的md5值
print(m.hexdigest())   #返回一个str类型的md5值
print("*"*30)
#方法二
print(hashlib.md5(b"root").hexdigest())
print("*"*30)
#方法三:
n=hashlib.new("md5")
n.update(b"root")
print(n.hexdigest())   #返回一个str类型的md5值
print("*"*30)
#方法四
x=hashlib.new("md5",b"root").hexdigest()
print(x)

import hashlib

print(hashlib.md5(b"root").hexdigest())
print(hashlib.sha1(b"root").hexdigest())
print(hashlib.sha224(b"root").hexdigest())
print(hashlib.sha256(b"root").hexdigest())
print(hashlib.sha384(b"root").hexdigest())
print(hashlib.sha512(b"root").hexdigest())
print(hashlib.sha3_224(b"root").hexdigest())
print(hashlib.sha3_256(b"root").hexdigest())
print(hashlib.sha3_384(b"root").hexdigest())
print(hashlib.sha3_512(b"root").hexdigest())

原文地址:https://www.cnblogs.com/csnd/p/11807600.html