Navan Chauhan
Navan Chauhan

Reputation: 447

Cannot encode sha1 to hex using hashlib in python3

I'm trying to encode a sha1 string into hex using hashlib in python3. I'm getting an attribute error.

The program runs successfully in python2.

gesture = file.read(hashlib.sha1().digest_size).encode('hex')
AttributeError: 'bytes' object has no attribute 'encode'

The File: ???ӷJ?*L??R?????T% (It is an unsalted SHA1 hashsum)

The file when read in binary mode: b'\xae\x93\xf0\xd3\xb7\x7fJ\xb4*L\x90\xdeR\x91\xa8\xa1\x9b\xb6T\x0f'

I am opening it in rb mode

Upvotes: 0

Views: 2138

Answers (2)

ForceBru
ForceBru

Reputation: 44888

You can do this:

with open("your_file", "rb") as f:
    Hash = f.read(hashlib.sha1().digest_size).hex()

Here hex is a method of the class bytes.

Upvotes: 1

tripleee
tripleee

Reputation: 189799

The hash itself will have a hexdigest method which produces the result you want. But the code you posted seems to attempt to apply a method to the return value from file.read(), not to the digest object. I'm guessing you probably mean something like

sha = hashlib.sha1()
buffer = file.read(sha.digest_size)
sha.update(buffer)
gesture = sha.hexdigest()

The attempt to use the digest size to specify how many bytes to read is suspicious, too. Normally you should read the entire file no matter how large or small it is; the digest.size is the length of the output from the SHA1 algorithm, not its input.

A more conventional way to do that would be

with open(filename, 'rb') as f:
    sha = hashlib(f.read())
gesture = sha.hexdigest()

If your goal is to read a binary representation of a SHA1 hash back into memory, hashlib doesn't support that directly. Hash algorithms are generally designed to make it impossible or at least extremely resource-intensive to reconstruct the original object from just the digest; but of course, if you save a hashlib object with pickle or similar, you should be able to read it back in and basically continue where you left off (though I believe there may be issues with transporting pickles between some Python versions).

If you just want the hex representation of a sequence of bytes, that's What's the correct way to convert bytes to a hex string in Python 3?

with open(filename, 'rb') as f:
    buffer = f.read()
hexbytes = buffer.hex()

Upvotes: 1

Related Questions