27 lines
957 B
Python
27 lines
957 B
Python
from tempfile import SpooledTemporaryFile
|
|
import cryptography.hazmat.primitives.hashes
|
|
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat, load_pem_public_key
|
|
|
|
|
|
def get_hash(data):
|
|
if isinstance(data, str):
|
|
data = data.encode('utf-8')
|
|
digest = cryptography.hazmat.primitives.hashes.Hash(cryptography.hazmat.primitives.hashes.SHA256())
|
|
digest.update(data)
|
|
return digest.finalize().hex()
|
|
|
|
|
|
def get_hex_from_temp_file(temp_file: SpooledTemporaryFile) -> bytes:
|
|
temp_file.seek(0)
|
|
file_data = temp_file.read()
|
|
return file_data
|
|
|
|
|
|
def encode_public_key(public_key):
|
|
if isinstance(public_key, str):
|
|
public_key = load_pem_public_key(public_key.encode('utf-8'))
|
|
|
|
return public_key.public_bytes(
|
|
encoding=cryptography.hazmat.primitives.serialization.Encoding.PEM,
|
|
format=cryptography.hazmat.primitives.serialization.PublicFormat.SubjectPublicKeyInfo
|
|
).decode('utf-8') |