Spaces:
Sleeping
Sleeping
File size: 1,208 Bytes
e7f02ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # encrypting & decrypting api keys
import base64
from Crypto.Cipher import AES
import os
class CryptoUtils:
def __init__(self):
self.SECRET_KEY = os.getenv("ENC_SECRET_KEY", "12345678901234567890123456789012").encode() # 32 bytes
self.BLOCK_SIZE = 16
class Encryptor(CryptoUtils):
def __init__(self):
super().__init__()
def pad(self,data: bytes) -> bytes:
pad_len = self.BLOCK_SIZE - len(data) % self.BLOCK_SIZE
return data + bytes([pad_len] * pad_len)
def encrypt_api_key(self,api_key: str) -> str:
iv = os.urandom(16)
cipher = AES.new(self.SECRET_KEY, AES.MODE_CBC, iv)
enc = cipher.encrypt(self.pad(api_key.encode()))
return base64.b64encode(iv + enc).decode()
class Decryptor(CryptoUtils):
def __init__(self):
super().__init__()
def unpad(self,data: bytes) -> bytes:
return data[:-data[-1]]
def decrypt_api_key(self,enc_key: str) -> str:
raw = base64.b64decode(enc_key)
iv, enc = raw[:16], raw[16:]
cipher = AES.new(self.SECRET_KEY, AES.MODE_CBC, iv)
dec = self.unpad(cipher.decrypt(enc))
return dec.decode() |