You can download this code by clicking the button below.
This code is now available for download.
This function randomly selects a password hashing algorithm from the supported ones in Passlib, hashes the given password using the chosen algorithm, and returns the hashed password.
Technology Stack : Passlib
Code Type : Password Hash Function Selection and Generation
Code Difficulty : Intermediate
import random
from passlib.context import CryptContext
from passlib.hash import argon2, bcrypt, sha256_crypt, md5_crypt
def random_password_hasher():
hash_types = [argon2, bcrypt, sha256_crypt, md5_crypt]
chosen_hash = random.choice(hash_types)
# Generate a random password
password = 'random_password'
# Hash the password
hashed_password = chosen_hash.hash(password)
return hashed_password