blog icon indicating copy to clipboard operation
blog copied to clipboard

Flask-Bcrypt使用笔记

Open nkypy opened this issue 9 years ago • 0 comments

首先导入Flask-Bcrypt app/init.py

from flask import Flask 

from flask_bcrypt import Bcrypt 

app = Flask(__name__) 

bcrypt = Bcrypt(app)

然后在app/models.py编辑

from sqlalchemy.ext.hybrid import hybrid_property
from . import bcrypt
class User(db.Model):
    ............
    _password = db.Column(db.String(128))

    @hybrid_property 
    def password(self):
        return self._password

    @password.setter
    def _set_password(self, plaintext):
        self._password = bcrypt.generate_password_hash(plaintext)

    def is_correct_passwd(self, plaintext):
        return bcrypt.check_password_hash(self._password, plaintext)

到此Bcrypt完成,如果希望修改安全等级,编辑config.py

BCRYPT_LEVEL = 12

nkypy avatar Sep 10 '15 06:09 nkypy