Mercurial > piecrust2
diff piecrust/admin/bcryptfallback.py @ 778:5e91bc0e3b4d
internal: Move admin panel code into the piecrust package.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 16 Jul 2016 15:02:24 +0200 |
parents | foodtruck/bcryptfallback.py@3885421c29a3 |
children | a9f4a6e60b0b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/piecrust/admin/bcryptfallback.py Sat Jul 16 15:02:24 2016 +0200 @@ -0,0 +1,50 @@ +import hashlib +import logging + + +print_warning = False +logger = logging.getLogger(__name__) + + +try: + from bcrypt import hashpw, gensalt +except ImportError: + print_warning = True + + def hashpw(password, *args, **kwargs): + return hashlib.sha512(password).hexdigest().encode('utf8') + + def gensalt(*args, **kwargs): + return b'' + + +try: + from flask.ext.bcrypt import Bcrypt +except ImportError: + print_warning = True + + def generate_password_hash(password): + return hashlib.sha512(password.encode('utf8')).hexdigest() + + def check_password_hash(reference, check): + check_hash = hashlib.sha512(check.encode('utf8')).hexdigest() + return check_hash == reference + + class SHA512Fallback(object): + is_fallback_bcrypt = True + + def __init__(self, app=None): + self.generate_password_hash = generate_password_hash + self.check_password_hash = check_password_hash + + def init_app(self, app): + pass + + Bcrypt = SHA512Fallback + + +if print_warning: + logging.warning("Bcrypt not available... falling back to SHA512.") + logging.warning("Run `pip install Flask-Bcrypt` for more secure " + "password hashing.") +