本篇介绍利用gmssl算法库中的sm3模块来进行hash值的计算。
import binascii
#导入国密算法sm4包
from gmssl import sm4,sm3
def sm3_hash(message:bytes):
"""
国密sm3加密
:param message: 消息值,bytes类型
:return: 哈希值
"""
msg_list = [i for i in message]
hash_hex = sm3.sm3_hash(msg_list)
print(hash_hex)
# bytes2hex(hash_hex);
hash_bytes = bytes.fromhex(hash_hex)
print(hash_bytes)
# return bytes.hash
# return hash
def bytes2hex(bytesData):
hex = binascii.hexlify(bytesData)
print(hex)
print(hex.decode())
return hex
# main
if __name__ == '__main__':
print("main begin");
message = b"123456" # bytes类型
sm3_hash(message);
运行结果:
% python3 sm3_t1.py
main begin
207cf410532f92a47dee245ce9b11ff71f578ebd763eb3bbea44ebd043d018fb
b’ |\xf4\x10S/\x92\xa4}\xee$\\xe9\xb1\x1f\xf7\x1fW\x8e\xbdv>\xb3\xbb\xeaD\xeb\xd0C\xd0\x18\xfb’