Switch to Fernet crypto as per gtank's recommendation

This commit is contained in:
Joseph Schorr 2016-01-12 14:08:41 -05:00
parent bd0a098282
commit b4bddacedb
3 changed files with 26 additions and 13 deletions

18
util/security/crypto.py Normal file
View file

@ -0,0 +1,18 @@
import base64
from cryptography.fernet import Fernet, InvalidToken
def encrypt_string(string, key):
""" Encrypts a string with the specified key. The key must be 32 raw bytes. """
f = Fernet(base64.urlsafe_b64encode(key))
return f.encrypt(string)
def decrypt_string(string, key):
""" Decrypts an encrypted string with the specified key. The key must be 32 raw bytes. """
f = Fernet(base64.urlsafe_b64encode(key))
try:
return f.decrypt(string)
except InvalidToken:
return None
except TypeError:
return None