bcryptの使い方の説明 flask login and password hashing using bcrypt Section 11, Lecture 42
bcryptの使い方の説明
最初にやる事は同じ
実際にpasswordをハッシュ化ってどんな風にやっているのか? Bcryptの'generate_password_hash'に注目する!これでハッシュ化する。
$ pip install flask_login $ pip install flask_bcrypt >>> from flask_bcrypt import Bcrypt >>> bcrypt = Bcrypt() >>> dir(bcrypt) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_log_rounds', 'check_password_hash', 'generate_password_hash', 'init_app'] >>> password = 'yamada' >>> hashed_password = bcrypt.generate_password_hash(password) >>> hashed_password b'$2b$12$8fz7XhXOSp7QHqoaFZNRWOwLZXWqgWkXfoHWyJwNrRCiRdlWC6FPy'
次に check_password_hash でパスワードがマッチするかを確認する。
hashed_passwordと設定したpasswordが一致すればTrueになる
>>> bcrypt.check_password_hash(hashed_password, password) True