mirror of
https://github.com/azaion/loader.git
synced 2026-04-22 08:16:33 +00:00
ec5d15b4e7
- Introduced `ApiClient` class for handling API interactions, including authentication and resource management. - Added `CDNManager` class for managing file uploads and downloads to/from a CDN. - Implemented security features for encryption and decryption of sensitive data. - Created supporting classes for credentials, user roles, and hardware information retrieval. - Established constants for configuration and logging. This commit lays the foundation for resource management and secure communication with the API and CDN services.
64 lines
2.2 KiB
Cython
64 lines
2.2 KiB
Cython
import base64
|
|
import hashlib
|
|
import os
|
|
from hashlib import sha384
|
|
from credentials cimport Credentials
|
|
from cryptography.hazmat.backends import default_backend
|
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
from cryptography.hazmat.primitives import padding
|
|
|
|
BUFFER_SIZE = 64 * 1024 # 64 KB
|
|
|
|
cdef class Security:
|
|
@staticmethod
|
|
cdef encrypt_to(input_bytes, key):
|
|
cdef bytes aes_key = hashlib.sha256(key.encode('utf-8')).digest()
|
|
iv = os.urandom(16)
|
|
|
|
cipher = Cipher(algorithms.AES(<bytes> aes_key), modes.CBC(iv), backend=default_backend())
|
|
encryptor = cipher.encryptor()
|
|
padder = padding.PKCS7(128).padder()
|
|
|
|
padded_plaintext = padder.update(input_bytes) + padder.finalize()
|
|
ciphertext = encryptor.update(padded_plaintext) + encryptor.finalize()
|
|
|
|
return iv + ciphertext
|
|
|
|
@staticmethod
|
|
cdef decrypt_to(ciphertext_with_iv_bytes, key):
|
|
cdef bytes aes_key = hashlib.sha256(key.encode('utf-8')).digest()
|
|
iv = ciphertext_with_iv_bytes[:16]
|
|
ciphertext_bytes = ciphertext_with_iv_bytes[16:]
|
|
|
|
cipher = Cipher(algorithms.AES(<bytes>aes_key), modes.CBC(<bytes>iv), backend=default_backend())
|
|
decryptor = cipher.decryptor()
|
|
|
|
decrypted_padded_bytes = decryptor.update(ciphertext_bytes) + decryptor.finalize()
|
|
|
|
unpadder = padding.PKCS7(128).unpadder()
|
|
plaintext_bytes = unpadder.update(decrypted_padded_bytes) + unpadder.finalize()
|
|
|
|
return bytes(plaintext_bytes)
|
|
|
|
@staticmethod
|
|
cdef get_hw_hash(str hardware):
|
|
cdef str key = f'Azaion_{hardware}_%$$$)0_'
|
|
return Security.calc_hash(key)
|
|
|
|
@staticmethod
|
|
cdef get_api_encryption_key(Credentials creds, str hardware_hash):
|
|
cdef str key = f'{creds.email}-{creds.password}-{hardware_hash}-#%@AzaionKey@%#---'
|
|
return Security.calc_hash(key)
|
|
|
|
@staticmethod
|
|
cdef get_resource_encryption_key():
|
|
cdef str key = '-#%@AzaionKey@%#---234sdfklgvhjbnn'
|
|
return Security.calc_hash(key)
|
|
|
|
@staticmethod
|
|
cdef calc_hash(str key):
|
|
str_bytes = key.encode('utf-8')
|
|
hash_bytes = sha384(str_bytes).digest()
|
|
cdef str h = base64.b64encode(hash_bytes).decode('utf-8')
|
|
return h
|