Created
June 18, 2016 21:19
-
-
Save loopDelicious/0c882c40daea4a7f542afb30d906f465 to your computer and use it in GitHub Desktop.
salt and hash passwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bcrypt | |
class User(db.Model): | |
"""User of website.""" | |
__tablename__ = "users" | |
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True) | |
email = db.Column(db.String(70), nullable=False) | |
password_hash = db.Column(db.String(60), nullable=False) | |
salt = db.Column(db.String(22), nullable=False) | |
def __init__(self, email, password): | |
"""Instantiate a user object within the User class with salted passwords.""" | |
self.email = email | |
self.salt = bcrypt.gensalt() # default work factor is 12, but you can customize complexity | |
self.password_hash = bcrypt.hashpw(password.encode('utf8'), self.salt.encode('utf8')) | |
def verify_password(self, password): | |
"""Verify user's password, a method that can be called on a user.""" | |
password_hash = bcrypt.hashpw(password.encode('utf8'), self.salt.encode('utf8')) | |
if self.password_hash == password_hash: | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment