You can download this code by clicking the button below.
This code is now available for download.
This function generates a password hash using the PBKDF2HMAC algorithm, which can specify salt and iteration count. It is used for secure password storage practices.
Technology Stack : cryptography, PBKDF2HMAC, SHA256, PBKDF2HMAC algorithm, salt, iteration count, password hash
Code Type : Function
Code Difficulty : Intermediate
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
from os import urandom
import json
def generate_password_hash(password, salt=None, iterations=100000):
if salt is None:
salt = urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=iterations,
backend=default_backend()
)
key = kdf.derive(password.encode())
return key, salt