Password Hashing with Passlib and PBKDF2_sha256

  • Share this:

Code introduction


This function uses the Passlib library's CryptContext and pbkdf2_sha256 scheme to encrypt a password. First, a password hashing context is created, and then a hash value of the password is generated using the context.


Technology Stack : Passlib, CryptContext, pbkdf2_sha256

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from passlib.context import CryptContext
from passlib.hash import pbkdf2_sha256

def hash_password(password):
    # Create a new password hashing context using PBKDF2 with SHA-256 hashing algorithm
    pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
    
    # Hash the password using the context
    hashed_password = pwd_context.hash(password)
    
    return hashed_password

# JSON representation of the code