SM3 hash and others
[Hazmat Home][Home]
In this case we will implement Python code to determine SHA1, SHA2, SHA3, Blake2, MD5 and SM3 hashes.
|
Outline
Code
The code is:
from cryptography.hazmat.primitives import hashes import binascii import sys st = "00" hex=False showhex="No" hashtype=0 def show_hash(type,data): digest = hashes.Hash(type) digest.update(data) res=digest.finalize() hex=binascii.b2a_hex(res).decode() b64=binascii.b2a_base64(res).decode() print (f"Message:\t{data.decode()}\nType:\t\t{type.name}\nDigest Size:\t{type.digest_size}\nBlock Size:\t{type.block_size}\n\nHex:\t{hex}\nBase64:\t{b64}") if (len(sys.argv)>1): st=str(sys.argv[1]) if (len(sys.argv)>2): hashtype=int(sys.argv[2]) h=hashes.SHA1() if (hashtype==0): h=hashes.SHA1() elif (hashtype==1): h=hashes.SHA512_224() elif (hashtype==2): h=hashes.SHA512_256() elif (hashtype==3): h=hashes.SHA224() elif (hashtype==4): h=hashes.SHA256() elif (hashtype==5): h=hashes.SHA384() elif (hashtype==6): h=hashes.SHA512() elif (hashtype==7): h=hashes.SHA3_224() elif (hashtype==8): h=hashes.SHA3_256() elif (hashtype==9): h=hashes.SHA3_384() elif (hashtype==10): h=hashes.SHA3_512() elif (hashtype==11): h=hashes.MD5() elif (hashtype==12): h=hashes.SM3() elif (hashtype==13): h=hashes.BLAKE2b(64) elif (hashtype==14): h=hashes.BLAKE2s(32) try: show_hash(h,st.encode()) except Exception as e: print(e)
and a sample run:
Message: Hello Type: blake2s Digest Size: 32 Block Size: 64 Hex: f73a5fbf881f89b814871f46e26ad3fa37cb2921c5e8561618639015b3ccbb71 Base64: 9zpfv4gfibgUhx9G4mrT+jfLKSHF6FYWGGOQFbPMu3E=