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

Navan Chauhan
  • 397
  • 6
  • 14
  • Does [this post](https://stackoverflow.com/questions/19342033/hashing-file-in-python-3) help you? – sashimi Apr 06 '19 at 12:36
  • 1
    @kekec No, I tried all of them. They failed – Navan Chauhan Apr 06 '19 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. – tripleee Apr 06 '19 at 12:48
  • @tripleee the file is an unsalted SHA1-hashsum, I'll update my question details with the same – Navan Chauhan Apr 06 '19 at 12:54

2 Answers2

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()
tripleee
  • 175,061
  • 34
  • 275
  • 318
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.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • Right, but it still operates on the output from `read`, not on a hash of the data. – tripleee Apr 06 '19 at 16:19
  • @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. – ForceBru Apr 06 '19 at 16:20
  • 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 Apr 06 '19 at 16:22
  • @tripleee, oh wait, I didn't see that part of your answer. Now my answer is a dupe of yours – ForceBru Apr 06 '19 at 16:24