17

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

Swetha
  • 423
  • 1
  • 3
  • 11
  • What do you mean by length of hash object? And where do you get 28? SHA1 is 40 chars long. – Abdul Fatir May 27 '16 at 12:03
  • and what do you mean `switch` and `case` that is surely not python... – domoarigato May 27 '16 at 12:11
  • the sha1 digest itself is 20 bytes - 40 characters hex encoded. maybe that's what is meant here. sha256 is 32 bytes (64 characters in hex). not sure what should be 14 - even md5 is 16 bytes. – domoarigato May 27 '16 at 12:14
  • 1
    Note that those are hex values that 0x14 is 20 and 0x20 is 32 so you are expecting binary hashes not hex encoded ones. Use `.digest()` not `.hexdigest()` to get the binary hash not the hex encoded one. – Dan D. May 27 '16 at 12:21
  • @domoarrigato switch case is in my c code. i am porting to python. – Swetha May 27 '16 at 12:43

1 Answers1

26

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.

Community
  • 1
  • 1
Abdul Fatir
  • 5,494
  • 5
  • 24
  • 54