密码安全性
使用Werkzeug计算密码散列值
generate_password_hash(password, method=’pbkdf2:sha256’, salt_length=8)
check_password_hash(hash, password)
from werkzeug.security import generate_password_hash, check_password_hash
class User(db.Model):
# ...
password_hash = db.Column(db.String(128))
@property
def password(self):
raise AttributeError('password is not a readable attribute')
@password.setter
def password(self, password):
self.password_hash = generate_password_hash(password)
def verify_password(self, password):
return check_password_hash(self.password_hash, password)