Files
jeb-utils/src/jeb_utils/crypto_utils.py
T
jakob.scheid a0036b6767 neue Datei: .gitignore
neue Datei:     README.md
	neue Datei:     pyproject.toml
	neue Datei:     src/jeb_utils/__init__.py
	neue Datei:     src/jeb_utils/auth_utils.py
	neue Datei:     src/jeb_utils/crypto_utils.py
	neue Datei:     src/jeb_utils/exceptions.py
	neue Datei:     src/jeb_utils/jeb_utils.py
	neue Datei:     src/jeb_utils/jebp_utils.py
	neue Datei:     src/jeb_utils/utils.py
2026-02-19 17:03:49 +01:00

35 lines
1.1 KiB
Python

from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
__all__ = [
'generate_keypair',
'serialize_public_key',
'deserialize_public_key',
'derive_aes_key'
]
def generate_keypair():
private_key = ec.generate_private_key(ec.SECP256R1())
return private_key, private_key.public_key()
def serialize_public_key(public_key: ec.EllipticCurvePublicKey) -> bytes:
return public_key.public_bytes(
encoding=serialization.Encoding.X962,
format=serialization.PublicFormat.UncompressedPoint,
)
def deserialize_public_key(data: bytes) -> ec.EllipticCurvePublicKey:
return ec.EllipticCurvePublicKey.from_encoded_point(
ec.SECP256R1(), data
)
def derive_aes_key(private_key, peer_public_key) -> bytes:
shared_secret = private_key.exchange(ec.ECDH(), peer_public_key)
return HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=None,
info=b'handshake',
).derive(shared_secret)