Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 742
Swetha Avatar asked May 27 '16 11:05

Swetha


People also ask

How do you make a SHA1 hash in Python?

Using update() In the earlier examples we have created the hash object initialized with the encoded string or byte string. There is another way to append the byte string to the sha1 hash object using update() method. You can use the update() multiple times to append the byte string or any other byte date.

How do I create a SHA-256 hash in Python?

For example: use sha256() to create a SHA-256 hash object. You can now feed this object with bytes-like objects (normally bytes ) using the update() method. At any point you can ask it for the digest of the concatenation of the data fed to it so far using the digest() or hexdigest() methods.

How is SHA1 generated?

SHA-1 works by feeding a message as a bit string of length less than 2 64 2^{64} 264 bits, and producing a 160-bit hash value known as a message digest. Note that the message below is represented in hexadecimal notation for compactness. There are two methods to encrypt messages using SHA-1.

How to initialize SHA1 hash object in Python?

There is another way to initialize sha1 hash object. It is by using the new () method. In the new () method, you have to specify the name of the algorithm you want to use as its first parameter. In addition, you can also add the encoded string as an optional second parameter. Here is an example More about hashlib module at Python docs.

How do I use Sha in Python?

SHA in Python. SHA, ( Secure Hash Algorithms ) are set of cryptographic hash functions defined by the language to be used for various applications such as password security etc. Some variants of it are supported by Python in the “hashlib” library. These can be found using “algorithms_guaranteed” function of hashlib.

What is SHA (Secure Hash Algorithms)?

Last Updated : 14 Feb, 2018 SHA, (Secure Hash Algorithms) are set of cryptographic hash functions defined by the language to be used for various applications such as password security etc. Some variants of it are supported by Python in the “ hashlib ” library. These can be found using “algorithms_guaranteed” function of hashlib.

What is the difference between SHA512 and SHA1?

This is one of the truncated version. SHA512 : This hash function belong to hash class SHA-2, the internal block size of it is 64 bits. SHA1 : The 160 bit hash function that resembles MD5 hash in working and was discontinued to be used seeing its security vulnerabilities.


1 Answers

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.

like image 136
Abdul Fatir Avatar answered Oct 21 '22 17:10

Abdul Fatir