pyhelpers.ops.hash_password(password, salt=None, salt_size=None, iterations=None, ret_hash=True, **kwargs)

Hash a password using hashlib.pbkdf2_hmac.

See also [OPS-HP-1].

  • password (str or int or float) – input as a password

  • salt (bytes or str) – random data; when salt=None (default), it is generated by os.urandom(), which depends on salt_size; see also [OPS-HP-2]

  • salt_size (int or None) – size of the function os.urandom(), i.e. the size of a random bytestring for cryptographic use; when salt_size=None (default), it uses 128

  • iterations (int or None) – size of the function hashlib.pbkdf2_hmac(), i.e. number of iterations of SHA-256; when salt_size=None (default), it uses 100000

  • ret_hash (bool) – whether to return the salt and key, defaults to True

  • kwargs – [optional] parameters of the function hashlib.pbkdf2_hmac()


(only when ret_hash=True) salt and key

Return type



>>> from pyhelpers.ops import hash_password, verify_password

>>> sk = hash_password('test%123', salt_size=16)  # salt and key

>>> salt_data = sk[:16].hex()
>>> key_data = sk[16:].hex()

>>> verify_password('test%123', salt=salt_data, key=key_data)