This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/util/security/crypto.py

19 lines
561 B
Python
Raw Normal View History

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)
2016-01-20 18:53:51 +00:00
def decrypt_string(string, key, ttl=None):
""" Decrypts an encrypted string with the specified key. The key must be 32 raw bytes. """
f = Fernet(base64.urlsafe_b64encode(key))
try:
2016-02-09 22:25:33 +00:00
return f.decrypt(str(string), ttl=ttl)
except InvalidToken:
return None
except TypeError:
return None