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?
hexdigest() : Returns the encoded data in hexadecimal format.
From the documentation for hash. hexdigest() : the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.
You might want to look into binascii
module, specifically hexlify
and unhexlify
functions.
In 2.x you can use str.decode('hex')
and str.encode('hex')
to convert between raw bytes and a hex string. In 3.x you need to use the binascii
module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With