Spaces:
Paused
Paused
File size: 1,885 Bytes
8d1819a |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import hashlib
import hmac
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
import os
def hash_data(data: str, password: str):
return hmac.new(password.encode(), data.encode(), hashlib.sha256).hexdigest()
def verify_data(data: str, hash: str, password: str):
return hash_data(data, password) == hash
def _generate_private_key():
return rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
def _generate_public_key(private_key: rsa.RSAPrivateKey):
return (
private_key.public_key()
.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
.hex()
)
def _decode_public_key(public_key: str) -> rsa.RSAPublicKey:
# Decode hex string back to bytes
pem_bytes = bytes.fromhex(public_key)
# Load the PEM public key
key = serialization.load_pem_public_key(pem_bytes)
if not isinstance(key, rsa.RSAPublicKey):
raise TypeError("The provided key is not an RSAPublicKey")
return key
def encrypt_data(data: str, public_key_pem: str):
return _encrypt_data(data.encode("utf-8"), _decode_public_key(public_key_pem))
def _encrypt_data(data: bytes, public_key: rsa.RSAPublicKey):
b = public_key.encrypt(
data,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None,
),
)
return b.hex()
def decrypt_data(data: str, private_key: rsa.RSAPrivateKey):
b = private_key.decrypt(
bytes.fromhex(data),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None,
),
)
return b.decode("utf-8")
|