Generating Hashed Passwords with Argon2

  • Share this:

Code introduction


This function generates a hashed password using the Argon2 algorithm from the Passlib library. If no salt is provided, a new one will be generated.


Technology Stack : Passlib, Argon2

Code Type : Password Hashing

Code Difficulty : Intermediate


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

def generate_hashed_password(password, salt=None):
    """
    This function generates a hashed password using the Argon2 algorithm from the Passlib library.
    If no salt is provided, a new one will be generated.
    """
    if salt is None:
        salt = argon2.generate_salt()
    context = CryptContext(schemes=["argon2"], deprecated="auto")
    hashed_password = context.hash(password, salt=salt)
    return hashed_password, salt

# JSON representation of the code                
              
Tags: