简体   繁体   中英

python hashlib.md5 escape characters

Following snippet presents basic usage of python hashlib.md5 (in py2.7).

>>> import hashlib
>>> m = hashlib.md5()
>>> m.update('phrase')
>>> m.digest()
'8Z\xa58^\x83\xef\xc5\xd8<u\x88\xee_\xb7\xe8'
>>> type(m.digest())
<type 'str'>

I've got two questions:

  1. how can I transform the hex representation into human-readable text?
  2. what is the reason for python not returning a simple hash like 385aa5385e83efc5d83c7588ee5fb7e8 ? What is this escaped hex representation used for?

For 1.: Just use m.hexdigest() .

See here for docs: http://docs.python.org/2/library/hashlib.html#hashlib.hash.hexdigest

You are looking at the binary representation of the hash digest. What you want is the hex digest, produced by hash.hexdigest() :

>>> import hashlib
>>> m = hashlib.md5()
>>> m.update('phrase')
>>> m.hexdigest()
'385aa5385e83efc5d83c7588ee5fb7e8'

Python gives you access to both the original binary value and the hexadecimal representation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM