mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 13:06:31 +00:00
fix auth, decryption, api interaction
This commit is contained in:
+30
-22
@@ -1,40 +1,48 @@
|
||||
# cython: language_level=3
|
||||
import hashlib
|
||||
import os
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives.hashes import Hash, SHA256
|
||||
from hashlib import sha384
|
||||
import base64
|
||||
|
||||
BUFFER_SIZE = 64 * 1024 # 64 KB
|
||||
|
||||
cdef class Security:
|
||||
|
||||
cdef encrypt_to(self, input_stream, output_stream, key):
|
||||
aes_key = hashlib.sha256(key.encode('utf-8')).digest()
|
||||
@staticmethod
|
||||
cdef encrypt_to(input_stream, key):
|
||||
cdef bytes aes_key = hashlib.sha256(key.encode('utf-8')).digest()
|
||||
iv = os.urandom(16)
|
||||
output_stream.write(iv) # Write IV to the output stream
|
||||
|
||||
cipher = Cipher(algorithms.AES(aes_key), modes.CFB(iv), backend=default_backend())
|
||||
cipher = Cipher(algorithms.AES(<bytes>aes_key), modes.CFB(iv), backend=default_backend())
|
||||
encryptor = cipher.encryptor()
|
||||
|
||||
# Read and encrypt in chunks
|
||||
cdef bytearray res = bytearray()
|
||||
res.extend(iv)
|
||||
while chunk := input_stream.read(BUFFER_SIZE):
|
||||
encrypted_data = encryptor.update(chunk)
|
||||
output_stream.write(encrypted_data)
|
||||
encrypted_chunk = encryptor.update(chunk)
|
||||
res.extend(encrypted_chunk)
|
||||
res.extend(encryptor.finalize())
|
||||
return res
|
||||
|
||||
final_data = encryptor.finalize()
|
||||
output_stream.write(final_data)
|
||||
@staticmethod
|
||||
cdef decrypt_to(input_stream, key):
|
||||
cdef bytes aes_key = hashlib.sha256(key.encode('utf-8')).digest()
|
||||
cdef bytes iv = input_stream.read(16)
|
||||
|
||||
cdef decrypt_to(self, input_stream, output_stream, key):
|
||||
aes_key = hashlib.sha256(key.encode('utf-8')).digest()
|
||||
iv = input_stream.read(16) # AES block size is 16 bytes
|
||||
|
||||
# Create cipher and decryptor
|
||||
cipher = Cipher(algorithms.AES(aes_key), modes.CFB(iv), backend=default_backend())
|
||||
decryptor = cipher.decryptor()
|
||||
cdef cipher = Cipher(algorithms.AES(<bytes>aes_key), modes.CBC(<bytes>iv), backend=default_backend())
|
||||
cdef decryptor = cipher.decryptor()
|
||||
|
||||
cdef bytearray res = bytearray()
|
||||
while chunk := input_stream.read(BUFFER_SIZE):
|
||||
decrypted_data = decryptor.update(chunk)
|
||||
output_stream.write(decrypted_data)
|
||||
decrypted_chunk = decryptor.update(chunk)
|
||||
res.extend(decrypted_chunk)
|
||||
res.extend(decryptor.finalize())
|
||||
return res
|
||||
|
||||
final_data = decryptor.finalize()
|
||||
output_stream.write(final_data)
|
||||
@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
|
||||
|
||||
Reference in New Issue
Block a user