Back to module index

Go to module by name

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.
sha384 and sha512 will be slow on 32 bit platforms.

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 datas that share a common
                 initial substring.

For example, to obtain the digest of the byte string 'Nobody inspects the
spammish repetition':

    >>> import hashlib
    >>> m = hashlib.md5()
    >>> m.update(b"Nobody inspects")
    >>> m.update(b" the spammish repetition")
    >>> m.digest()
    b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'

More condensed:

    >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'


Classes

blake2b

Return a new BLAKE2b hash object.
copy(self, /)

  Return a copy of the hash object.
digest(self, /)

  Return the digest value as a bytes object.
hexdigest(self, /)

  Return the digest value as a string of hexadecimal digits.
update(self, data, /)

  Update this hash object's state with the provided bytes-like object.
MAX_DIGEST_SIZE = 64
MAX_KEY_SIZE = 64
PERSON_SIZE = 16
SALT_SIZE = 16
block_size = <attribute 'block_size' of '_blake2.blake2b' objects>
digest_size = <attribute 'digest_size' of '_blake2.blake2b' objects>
name = <attribute 'name' of '_blake2.blake2b' objects>

blake2s

Return a new BLAKE2s hash object.
copy(self, /)

  Return a copy of the hash object.
digest(self, /)

  Return the digest value as a bytes object.
hexdigest(self, /)

  Return the digest value as a string of hexadecimal digits.
update(self, data, /)

  Update this hash object's state with the provided bytes-like object.
MAX_DIGEST_SIZE = 32
MAX_KEY_SIZE = 32
PERSON_SIZE = 8
SALT_SIZE = 8
block_size = <attribute 'block_size' of '_blake2.blake2s' objects>
digest_size = <attribute 'digest_size' of '_blake2.blake2s' objects>
name = <attribute 'name' of '_blake2.blake2s' objects>

Functions

md5

openssl_md5(string=b'', *, usedforsecurity=True)

  Returns a md5 hash object; optionally initialized with a string

new

__hash_new(name, data=b'', **kwargs)

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

pbkdf2_hmac

pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)

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

scrypt

scrypt(password, *, salt=None, n=None, r=None, p=None, maxmem=0, dklen=64)

  scrypt password-based key derivation function.

sha1

openssl_sha1(string=b'', *, usedforsecurity=True)

  Returns a sha1 hash object; optionally initialized with a string

sha224

openssl_sha224(string=b'', *, usedforsecurity=True)

  Returns a sha224 hash object; optionally initialized with a string

sha256

openssl_sha256(string=b'', *, usedforsecurity=True)

  Returns a sha256 hash object; optionally initialized with a string

sha384

openssl_sha384(string=b'', *, usedforsecurity=True)

  Returns a sha384 hash object; optionally initialized with a string

sha3_224

openssl_sha3_224(string=b'', *, usedforsecurity=True)

  Returns a sha3-224 hash object; optionally initialized with a string

sha3_256

openssl_sha3_256(string=b'', *, usedforsecurity=True)

  Returns a sha3-256 hash object; optionally initialized with a string

sha3_384

openssl_sha3_384(string=b'', *, usedforsecurity=True)

  Returns a sha3-384 hash object; optionally initialized with a string

sha3_512

openssl_sha3_512(string=b'', *, usedforsecurity=True)

  Returns a sha3-512 hash object; optionally initialized with a string

sha512

openssl_sha512(string=b'', *, usedforsecurity=True)

  Returns a sha512 hash object; optionally initialized with a string

shake_128

openssl_shake_128(string=b'', *, usedforsecurity=True)

  Returns a shake-128 variable hash object; optionally initialized with a string

shake_256

openssl_shake_256(string=b'', *, usedforsecurity=True)

  Returns a shake-256 variable hash object; optionally initialized with a string

Other members

algorithms_available = {'sha3_224', 'sha3_512', 'md5-sha1', 'sha512_256', 'md5', 'shake_128', 'sha512', 'sha3_384', 'sha512_224', 'blake2s', 'ripemd160', 'sha1', 'sha3_256', 'blake2b', 'sha256', 'shake_256', 'sha224', 'sm3', 'sha384'}
algorithms_guaranteed = {'sha3_224', 'sha3_512', 'sha3_384', 'sha1', 'sha3_256', 'blake2b', 'blake2s', 'md5', 'shake_128', 'sha256', 'sha512', 'shake_256', 'sha384', 'sha224'}