Search code examples
pythonhashsha256hashlib

What does hexdigest do in Python?


We need such a code for hashing:

from hashlib import sha256
Hash = sha256(b"hello").hexdigest()
#Hash = '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'

hexdigest seems to be doing the main thing, because without it we will get the following result:

Hash = sha256(b"hello")
#Hash = <sha256 HASH object @ 0x000001E92939B950>

The use of hexdigest is mandatory because if it is not used, another output will be obtained, but what does it do?


Solution

  • The actual digest is a really big number. It is conventionally represented as a sequence of hex digits, as we humans aren't very good at dealing with numbers with more than a handful of digits (and hex has the advantage that it reveals some types of binary patterns really well; for example, you'd be hard pressed to reason about a number like 4,262,789,120 whereas its hex representation FE150000 readily reveals that the low 16 bits are all zeros) but the object is more than just a number; it's a class instance with methods which allow you e.g. to add more data in chunks, so that you can calculate the digest of a large file or a stream of data successively, without keeping all of it in memory. You can think of the digest object as a collection of states which permit this operation to be repeated many times, and the hex digest method as a way to query its state at the current point in the input stream.

    You could argue that the interface could be different - for example, str(Hash) could produce the hex representation; but this only pushes the problem to a different, and arguably more obscure, corner.

    For completeness, hexdigest is not well-defined in Python generally. It is the name of a set of methods within the hashlib module in the standard library, and this exposition discusses that particular use case. Other libraries could have a method with the same name but completely different semantics; or they could have a method with the same purpose but a completely different name.