How to translate from a hexdigest to a digest and vice-versa?

esac picture esac · Oct 22, 2011 · Viewed 15.4k times · Source

I want to store hashes as binary (64 bytes). But for any type of API (web service) I would want to pass them around as strings. hashlib.hexdigest() will give me a string, and hashlib.digest() will give me the binary. But if, for example, I read in the binary version from disk, how would I convert it to a string? And if I read in the string from a web service, how would I convert it to binary?

Answer

lormus picture lormus · Oct 22, 2011

You might want to look into binascii module, specifically hexlify and unhexlify functions.