Search code examples
pythonasciiencode

Pass raw bytes as a literal to hashlib.md5


Looks like python3 treats regular strings as unicode...

import hashlib
h= hashlib.md5()
h.update ('abcd')

cause the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Unicode-objects must be encoded before hashing

forcing me to do encode it before:

import hashlib
h= hashlib.md5()
h.update ('abcd'.encode ('ascii', 'replace'))

which is tedious since the structure occurs several dozens of time in the program.

I was wondering if there is an alternative to not use encode everywhere in the program.


Solution

  • You can define a byte literal using the b prefix. i.e.

    import hashlib
    
    h = hashlib.md5()
    h.update(b"abcd")
    

    References