2017-08-18 15:39:39 +01:00
|
|
|
from flask_bcrypt import generate_password_hash, check_password_hash
|
2016-01-19 11:38:29 +00:00
|
|
|
|
2016-02-16 15:28:30 +00:00
|
|
|
from itsdangerous import URLSafeSerializer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Encryption:
|
|
|
|
|
def init_app(self, app):
|
|
|
|
|
self.serializer = URLSafeSerializer(app.config.get('SECRET_KEY'))
|
|
|
|
|
self.salt = app.config.get('DANGEROUS_SALT')
|
|
|
|
|
|
|
|
|
|
def encrypt(self, thing_to_encrypt):
|
2016-02-16 17:42:04 +00:00
|
|
|
return self.serializer.dumps(thing_to_encrypt, salt=self.salt)
|
2016-02-16 15:28:30 +00:00
|
|
|
|
|
|
|
|
def decrypt(self, thing_to_decrypt):
|
|
|
|
|
return self.serializer.loads(thing_to_decrypt, salt=self.salt)
|
|
|
|
|
|
2016-01-19 11:38:29 +00:00
|
|
|
|
|
|
|
|
def hashpw(password):
|
|
|
|
|
return generate_password_hash(password.encode('UTF-8'), 10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_hash(password, hashed_password):
|
|
|
|
|
# If salt is invalid throws a 500 should add try/catch here
|
|
|
|
|
return check_password_hash(hashed_password, password)
|