Hash a password with passlib

First, install passlib if you haven't already: pip install passlib

from passlib.hash import sha256_crypt

hash = sha256_crypt.encrypt("mypassword")

print(hash)
$5$rounds=110000$abfDfonbiHZa6HoJ$KjnU2X7eR6ZPZIQefg/NSelvVqcjZmXWekVJ5vqEhR6
print(sha256_crypt.verify("mypassword", hash))
True
print(sha256_crypt.verify("anotherpassword", hash))
False
class User():

    def __init__(self, username, password):
        self.username = username
        self.pw_hash = sha256_crypt.encrypt(password)

    def verify(self, password):
        return sha256_crypt.verify(password, self.pw_hash)

new_user = User('joe', 'somepassword')

print(new_user.verify('somepassword'))
True

About this Entry

Related Content


Comments