1
0
mirror of https://github.com/quay/quay.git synced 2026-01-27 18:42:52 +03:00
Files
quay/util/security/crypto.py
2020-02-05 19:55:07 -08:00

29 lines
565 B
Python

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(key)
return f.encrypt(string)
def decrypt_string(string, key, ttl=None):
"""
Decrypts an encrypted string with the specified key.
The key must be 32 raw bytes.
"""
f = Fernet(key)
try:
return f.decrypt(str(string), ttl=ttl)
except InvalidToken:
return None
except TypeError:
return None