Skip to content

Instantly share code, notes, and snippets.

@loopDelicious
Created June 18, 2016 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loopDelicious/0c882c40daea4a7f542afb30d906f465 to your computer and use it in GitHub Desktop.
Save loopDelicious/0c882c40daea4a7f542afb30d906f465 to your computer and use it in GitHub Desktop.
salt and hash passwords
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