Password Hashing with Passlib#s CryptContext

  • Share this:

Code introduction


This function uses the CryptContext class from the Passlib library to create a password hashing context that supports multiple password hashing schemes. The function first hashes the password using the bcrypt scheme and then verifies the input password against the stored hash.


Technology Stack : Passlib

Code Type : Password Hashing

Code Difficulty : Intermediate


                
                    
from passlib.context import CryptContext
from passlib.pwd import hash_password, verify_password

def secure_password_hashing(password):
    # Initialize a password hashing context
    pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
    
    # Hash a password for the first time, returns a hashed password
    hashed_password = pwd_context.hash(password)
    
    # Verify a password against a stored hash
    password_check = pwd_context.verify(password, hashed_password)
    
    return hashed_password, password_check                
              
Tags: