hashlib - Secure hashes and message digests
Contents
hashlib - Secure hashes and message digests
hashlib
provides an interface to many different secure hash and message digest algorithms.
hashlib Code Examples
List of hashing algorithms available
>>> import hashlib
>>> hashlib.algorithms_available
{'sha3_384', 'sha1', 'sha256', 'sha512', 'mdc2', 'sha512_256', 'sha224', 'sm3',
'shake_256', 'whirlpool', 'sha384', 'blake2b', 'sha512_224', 'md5', 'sha3_256',
'ripemd160', 'sha3_512', 'shake_128', 'blake2s', 'md5-sha1', 'sha3_224', 'md4'}
>>> hashlib.algorithms_guaranteed
{'sha3_384', 'blake2b', 'sha1', 'sha224', 'sha3_512', 'md5', 'shake_128', 'sha3_256',
'sha256', 'blake2s', 'shake_256', 'sha512', 'sha384', 'sha3_224'}
Generate a sha256 hash of the string Hello World!
import hashlib
m = hashlib.sha256()
m.update(b"Hello World!")
print(m.digest())
# output
b'\x7f\x83\xb1e\x7f\xf1\xfcS\xb9-\xc1\x81H\xa1\xd6]\xfc-K\x1f\xa3\xd6w(J\xdd\xd2\x00\x12m\x90i'
Generate a sha1 hash and print in string format
import hashlib
m = hashlib.sha1()
m.update("The quick brown fox jumps over the lazy dog")
print(m.hexdigest())
# output
2fd4e1c67a2d28fced849ee1bb76e7391b93eb12