hash – What does hexdigest do in Python?

hash – What does hexdigest do in Python?

The actual digest is a really big number. It is conventionally represented as a sequence of hex digits, as we humans arent 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, youd 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; its 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(obj) could produce the hex representation; but this only pushes the problem to a different, and arguably more obscure, corner.

hash – What does hexdigest do in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *