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?
You could start with the string version to pass around and display:
>>> import hashlib
>>> string_version = hashlib.md5(b'hello world').hexdigest()
Convert it to binary to write it to disk:
>>> save_as_binary = string_version.encode('utf-8')
>>> print(save_as_binary)
b'5eb63bbbe01eeed093cb22bb8f5acdc3'
When reading it back from disk, convert it back to a string:
>>> back_to_string = save_as_binary.decode('utf-8')
>>> print(back_to_string)
5eb63bbbe01eeed093cb22bb8f5acdc3
string_version.encode('utf-8')
does not deliver the binary interpretation of the hexdigest. It just delivers a binary string of the hex string. save_as_binary is not the same as digest() which was asked for. –
Amesace bytes.fromhex()
. –
Soubise 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.
You could start with the string version to pass around and display:
>>> import hashlib
>>> string_version = hashlib.md5(b'hello world').hexdigest()
Convert it to binary to write it to disk:
>>> save_as_binary = string_version.encode('utf-8')
>>> print(save_as_binary)
b'5eb63bbbe01eeed093cb22bb8f5acdc3'
When reading it back from disk, convert it back to a string:
>>> back_to_string = save_as_binary.decode('utf-8')
>>> print(back_to_string)
5eb63bbbe01eeed093cb22bb8f5acdc3
hashlib.md5(b'hello world').hexdigest().decode('hex') == hashlib.md5(b'hello world').digest()
–
Okelley string_version.encode('utf-8')
does not deliver the binary interpretation of the hexdigest. It just delivers a binary string of the hex string. save_as_binary is not the same as digest() which was asked for. –
Amesace Some existing answers here are missing the point. The digest is bytes and the hexdigest is a str:
>>> from hashlib import md5
>>> h = md5(b"hello world")
>>> h.digest()
b'^\xb6;\xbb\xe0\x1e\xee\xd0\x93\xcb"\xbb\x8fZ\xcd\xc3'
>>> h.hexdigest()
'5eb63bbbe01eeed093cb22bb8f5acdc3'
Going from digest (bytes) to hexdigest (str), use bytes.hex
:
>>> h.digest().hex()
'5eb63bbbe01eeed093cb22bb8f5acdc3'
Going from hexdigest (str) to digest (bytes), use bytes.fromhex
:
>>> bytes.fromhex(h.hexdigest())
b'^\xb6;\xbb\xe0\x1e\xee\xd0\x93\xcb"\xbb\x8fZ\xcd\xc3'
© 2022 - 2024 — McMap. All rights reserved.
hashlib.md5(b'hello world').hexdigest().decode('hex') == hashlib.md5(b'hello world').digest()
– Okelley