How to hash a password in Python

1 Answer

0 votes
import hashlib, os, base64

def hash_password(password):
    salt = os.urandom(16)
    hash_bytes = hashlib.pbkdf2_hmac(
        "sha256",
        password.encode(),
        salt,
        100000
    )
    return base64.b64encode(salt).decode() + ":" + base64.b64encode(hash_bytes).decode()


stored_hash = hash_password("SecurePassword123!@")
print("Stored hash:", stored_hash)



'''
run:
    
Stored hash: OwoAqK102Z9L3mx31lfzHQ==:qtEP5KAN8t8+vGBVZuuPv5YSaIWST6iN3oasLqvTGo8=


'''

 



answered 11 hours ago by avibootz

Related questions

1 answer 8 views
1 answer 8 views
1 answer 10 views
1 answer 197 views
197 views asked Sep 14, 2017 by avibootz
1 answer 190 views
190 views asked Sep 14, 2017 by avibootz
...