hash_password
- pyhelpers.ops.hash_password(password, salt=None, salt_size=None, iterations=None, ret_hash=True, **kwargs)[source]
Hash a password using hashlib.pbkdf2_hmac.
See also [OPS-HP-1].
- Parameters:
password (str | int | float | bytes) – input as a password
salt (bytes | str) – random data; when
salt=None
(default), it is generated by os.urandom(), which depends onsalt_size
; see also [OPS-HP-2]salt_size (int | None) –
size
of the function os.urandom(), i.e. the size of a random bytestring for cryptographic use; whensalt_size=None
(default), it uses128
iterations (int | None) –
size
of the function hashlib.pbkdf2_hmac(), i.e. number of iterations of SHA-256; whensalt_size=None
(default), it uses100000
ret_hash (bool) – whether to return the salt and key, defaults to
True
kwargs – [optional] parameters of the function hashlib.pbkdf2_hmac()
- Returns:
(only when
ret_hash=True
) salt and key- Return type:
bytes
Examples:
>>> from pyhelpers.ops import hash_password, verify_password >>> test_pwd = 'test%123' >>> salt_size_ = 16 >>> sk = hash_password(password=test_pwd, salt_size=salt_size_) # salt and key >>> salt_data, key_data = sk[:salt_size_].hex(), sk[salt_size_:].hex() >>> verify_password(password=test_pwd, salt=salt_data, key=key_data) True >>> test_pwd = b'test%123' >>> sk = hash_password(password=test_pwd) >>> salt_data, key_data = sk[:128].hex(), sk[128:].hex() >>> verify_password(password=test_pwd, salt=salt_data, key=key_data) True