0

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

4
  • Does this post help you? Commented Apr 6, 2019 at 12:36
  • 1
    @kekec No, I tried all of them. They failed Commented Apr 6, 2019 at 12:47
  • Displaying the contents o| a file as a string without also mentioning how the string is encoded is ambiguous at best, and often simply useless. Perhaps a hex dump of the file would be more helpful; but I guess we actually don't need to see your precise input. Commented Apr 6, 2019 at 12:48
  • @tripleee the file is an unsalted SHA1-hashsum, I'll update my question details with the same Commented Apr 6, 2019 at 12:54

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Right, but it still operates on the output from read, not on a hash of the data.
@tripleee, right. As far as I understand, the OP wants to read a hash that's stored in binary from a file and display it as hex.
Yeah, that's in the last passage of my answer. But perhaps it makes sense to repost as a separate answer now that the question has been somewhat clarified.
@tripleee, oh wait, I didn't see that part of your answer. Now my answer is a dupe of yours
1

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()

6 Comments

... And/or perhaps I misunderstand what file is here.
When I run your code, the gesture is 8b6c1d9b8b6ab090b92b4acf4d5ca6b59adedbfd whereas when I run my own code through python, the result is ae93f0d3b77f4ab42a4c90de5291a8a19bb6540f
Python referring to python2
The result you report from Python 2 is the file itself, not its digest; like I explain in my answer, your original code has multiple bugs.
@Navan See updated answer now with several additional speculations.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.