简体   繁体   中英

how to create a sha1 hash in python

My objective is to perform "Hashsigning" using smart card in python. there are hashlib's used but there is no specific SHA1 or SHA256 functions in python. My Work:

hash_object = hashlib.sha1(b'HelWorld')
pbHash = hash_object.hexdigest()

but the length of the hash object I get is 28 rather i should get 14 or 20 so that i can switch on condition as

 switch ( dwHashLen )
{
case 0x14: // SHA1 hash
             call scard transmit
case 0x20: // SHA256 hash
}

Any help is appreciated. Thank you in advance

You're actually getting 40, which in hex is 0x28 . Decode the hash in hexadecimal to ASCII as follows

>>> import hashlib
>>> hash_object = hashlib.sha1(b'HelWorld')
>>> pbHash = hash_object.hexdigest()
>>> length = len(pbHash.decode("hex"))
>>> print length
20

Or simply use digest instead of hexdigest as Dan D suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM