all Python 3hashlibfunction
hashlib.md5()
hashing
Creates an MD5 hash object. Fast, but not cryptographically strong — do not use it for passwords. Fine for file checksums.
Syntax
hashlib.md5(data=b'', *, usedforsecurity=True)
The hash object takes bytes only: hashlib.md5('text') raises TypeError right away — encode the string first. Data accumulates, so calling update() several times gives the same result as one call on the concatenated input, and hexdigest() can be read repeatedly without resetting anything. That is why a large file is hashed chunk by chunk instead of being loaded whole.
Examples
import hashlib
h = hashlib.md5(b'hello world')
print(h.hexdigest()) # → 5eb63bbbe01eeed093cb22bb8f5acdc3
import hashlib
# хеш файла: файл читается в бинарном режиме
with open('file.txt', 'wb') as f:
f.write(b'hello world')
with open('file.txt', 'rb') as f:
print(hashlib.md5(f.read()).hexdigest()) # → 5eb63bbbe01eeed093cb22bb8f5acdc3
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy