Mercurial > piecrust2
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 777:8d633ca59bc5 | 778:5e91bc0e3b4d |
|---|---|
| 1 import hashlib | |
| 2 import logging | |
| 3 | |
| 4 | |
| 5 print_warning = False | |
| 6 logger = logging.getLogger(__name__) | |
| 7 | |
| 8 | |
| 9 try: | |
| 10 from bcrypt import hashpw, gensalt | |
| 11 except ImportError: | |
| 12 print_warning = True | |
| 13 | |
| 14 def hashpw(password, *args, **kwargs): | |
| 15 return hashlib.sha512(password).hexdigest().encode('utf8') | |
| 16 | |
| 17 def gensalt(*args, **kwargs): | |
| 18 return b'' | |
| 19 | |
| 20 | |
| 21 try: | |
| 22 from flask.ext.bcrypt import Bcrypt | |
| 23 except ImportError: | |
| 24 print_warning = True | |
| 25 | |
| 26 def generate_password_hash(password): | |
| 27 return hashlib.sha512(password.encode('utf8')).hexdigest() | |
| 28 | |
| 29 def check_password_hash(reference, check): | |
| 30 check_hash = hashlib.sha512(check.encode('utf8')).hexdigest() | |
| 31 return check_hash == reference | |
| 32 | |
| 33 class SHA512Fallback(object): | |
| 34 is_fallback_bcrypt = True | |
| 35 | |
| 36 def __init__(self, app=None): | |
| 37 self.generate_password_hash = generate_password_hash | |
| 38 self.check_password_hash = check_password_hash | |
| 39 | |
| 40 def init_app(self, app): | |
| 41 pass | |
| 42 | |
| 43 Bcrypt = SHA512Fallback | |
| 44 | |
| 45 | |
| 46 if print_warning: | |
| 47 logging.warning("Bcrypt not available... falling back to SHA512.") | |
| 48 logging.warning("Run `pip install Flask-Bcrypt` for more secure " | |
| 49 "password hashing.") | |
| 50 |
