PyDocs

Cryptographic Services

Cryptographic Services

hashlib

hashlib module - A common interface to many hash functions.

new(name, data=b'', **kwargs) - returns a new hash object implementing the
                                given hash function; initializing the hash
                                using the given binary data.

Named constructor functions are also available, these are faster
than using new(name):

md5(), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(),
sha3_224(), sha3_256(), sha3_384(), sha3_512(), shake_128(), and shake_256().

More algorithms may be available on your platform but the above are guaranteed
to exist.  See the algorithms_guaranteed and algorithms_available attributes
to find out what algorithm names can be passed to new().

NOTE: If you want the adler32 or crc32 hash functions they are available in
the zlib module.

Choose your hash function wisely.  Some have known collision weaknesses,
while others may be slower depending on the CPU architecture.

Hash objects have these methods:
 - update(data): Update the hash object with the bytes in data. Repeated calls
                 are equivalent to a single call with the concatenation of all
                 the arguments.
 - digest():     Return the digest of the bytes passed to the update() method
                 so far as a bytes object.
 - hexdigest():  Like digest() except the digest is returned as a string
                 of double length, containing only hexadecimal digits.
 - copy():       Return a copy (clone) of the hash object. This can be used to
                 efficiently compute the digests of data that share a common
                 initial substring.

Assuming that Python has been built with SHA-2 support, the SHA-256 digest
of the byte string b'Nobody inspects the spammish repetition' is computed
as follows:

    >>> import hashlib
    >>> m = hashlib.sha256()
    >>> m.update(b"Nobody inspects")
    >>> m.update(b" the spammish repetition")
    >>> m.digest()  # doctest: +ELLIPSIS
    b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7...'

More condensed:

    >>> hashlib.sha256(b"Nobody inspects the spammish repetition").hexdigest()
    '031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
Symbols in hashlib

a

algorithms_available
A set containing the names of the hash algorithms that are available in the running Python interpreter.
algorithms_guaranteed
A set containing the names of the hash algorithms guaranteed to be supported by this module on all platforms.

b

blake2b
Return a new BLAKE2b hash object.
blake2s
Return a new BLAKE2s hash object.

f

file_digest
Hash the contents of a file-like object.

h

hash.block_size
The internal block size of the hash algorithm in bytes.
hash.copy
Return a copy (“clone”) of the hash object.
hash.digest
Return the digest of the data passed to the update() method so far.
hash.digest_size
The size of the resulting hash in bytes.
hash.hexdigest
Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits.
hash.name
The canonical name of this hash, always lowercase and always suitable as a parameter to new() to create another hash of this type.
hash.update
Update the hash object with the bytes-like object.

m

md5
Returns a md5 hash object; optionally initialized with a string

n

new
new(name, data=b'') - Return a new hashing object using the named algorithm; optionally initialized with data (which must be a bytes-like object).

p

pbkdf2_hmac
Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as pseudorandom function.

s

scrypt
scrypt password-based key derivation function.
sha1
Returns a sha1 hash object; optionally initialized with a string
sha224
Returns a sha224 hash object; optionally initialized with a string
sha256
Returns a sha256 hash object; optionally initialized with a string
sha3_224
Returns a sha3-224 hash object; optionally initialized with a string
sha3_256
Returns a sha3-256 hash object; optionally initialized with a string
sha3_384
Returns a sha3-384 hash object; optionally initialized with a string
sha3_512
Returns a sha3-512 hash object; optionally initialized with a string
sha384
Returns a sha384 hash object; optionally initialized with a string
sha512
Returns a sha512 hash object; optionally initialized with a string
shake_128
Returns a shake-128 variable hash object; optionally initialized with a string
shake_256
Returns a shake-256 variable hash object; optionally initialized with a string
shake.digest
Return the digest of the data passed to the update() method so far.
shake.hexdigest
Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits.