Python Hashlib模块
Hashlib模块提供了许多供我们调用的hash算法,主要有:
1、md5
2、SHA系列:sha1, sha224, sha256, sha384, sha512
# -*- coding:utf-8 -*-
import hashlib
result = hashlib.new('md5','张三丰'.encode()) # 生成一个hash对象
print(result.hexdigest()) # 返回Hash算法计算得到的值(str类型)
result.update('白居易'.encode())
print(result.hexdigest())
print('-'*50)
"""
hash算法:
1、md5
2、SHA系列:sha1, sha224, sha256, sha384, sha512
"""
result1 = hashlib.new('md5','雪白的合果芋'.encode())
a = result1.hexdigest()
print(a,len(a))
result2 = hashlib.sha256('李白'.encode()) # 生成一个使用sha256算法的hash对象
b = result2.hexdigest()
print(b,len(b))
print('-'*50)
""" ######## md5 ######## """
hash = hashlib.md5()
hash.update(bytes('admin', encoding='utf-8'))
u = hash.hexdigest()
print("md5加密结果是:%s,其长度是%d"%(u,len(u)))
# # print(hash.digest()) # 返回Hash算法计算得到的值(bytes类型)
print('-'*50)
""" ######## sha1 ######## """
hash = hashlib.sha1()
hash.update(bytes('admin', encoding='utf-8'))
v = hash.hexdigest()
print("sha1加密结果是:%s,其长度是%d"%(v,len(v)))
print('-'*50)
""" ######## sha224 ######## """
hash = hashlib.sha224()
hash.update(bytes('admin', encoding='utf-8'))
w = hash.hexdigest()
print("sha224加密结果是:%s,其长度是%d"%(w,len(w)))
print('-'*50)
""" ######## sha256 ######## """
hash = hashlib.sha256()
hash.update(bytes('admin', encoding='utf-8'))
x = hash.hexdigest()
print("sha256加密结果是:%s,其长度是%d"%(x,len(x)))
print('-'*50)
""" ######## sha384 ######## """
hash = hashlib.sha384()
hash.update(bytes('admin', encoding='utf-8'))
y = hash.hexdigest()
print("sha384加密结果是:%s,其长度是%d"%(y,len(y)))
print('-'*50)
""" ######## sha512 ######## """
hash = hashlib.sha512()
hash.update(bytes('admin', encoding='utf-8'))
z = hash.hexdigest()
print("sha512加密结果是:%s,其长度是%d"%(z,len(z)))
程序运行结果如下图所示: