Password Hashing with bcrypt in Python

  • Share this:

Code introduction


This function uses the CryptContext class from the Passlib library to create a password hasher and generates a hash value for the password using the bcrypt algorithm.


Technology Stack : Passlib, bcrypt

Code Type : Password hash function

Code Difficulty : Intermediate


                
                    
from passlib.context import CryptContext
import random

def hash_password(password):
    # Initialize a new CryptContext object with a default hash algorithm
    context = CryptContext(schemes=["bcrypt"], deprecated="auto")
    
    # Generate a salt and hash the password
    hashed = context.hash(password)
    
    return hashed

# JSON representation of the code                
              
Tags: