M1-PCA-Project/source/models/Elector.py
biloute02 cc243e5163 Function to end election.
Convert type of pubkey from bytes to RSAPublicKey.
2024-07-04 19:31:20 +02:00

47 lines
1.7 KiB
Python

from typing import Optional
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
class Elector:
# [
# {
# "name": "Bob",
# "votant": (pub_key_bob, Hash(empreinte_bob)),
# "mandataire": (b"", b"")
# },
# {
# "name": "Alice",
# "votant": (pub_key_alice, Hash(empreinte_alice)),
# "mandataire": (pub_key_Eve, Hash(empreinte_eve)), # Eve peut voter pour Alice.
# }
# ]
def __init__(self,
name: str,
public_key_elector: RSAPublicKey,
fingerprint_elector: str,
public_key_mandataire: Optional[RSAPublicKey] = None,
fingerprint_mandataire: Optional[str] = None):
self.name = name
self.public_key_elector = public_key_elector
if public_key_mandataire is None:
self.public_key_mandataire = public_key_elector
else:
self.public_key_mandataire = public_key_mandataire
digest = hashes.Hash(hashes.SHA256())
digest.update(bytes(fingerprint_elector, 'utf-8'))
self.hashed_fingerprint_elector: bytes = digest.finalize()
if fingerprint_mandataire is None:
self.hashed_fingerprint_mandataire = self.hashed_fingerprint_elector
else:
digest = hashes.Hash(hashes.SHA256())
digest.update(bytes(fingerprint_mandataire, 'utf-8'))
self.hashed_fingerprint_mandataire: bytes = digest.finalize()
def set_mandataire(self, public_key: bytes, hashed_fingerprint: bytes):
self.public_key_mandataire = public_key
self.hashed_fingerprint_mandataire = hashed_fingerprint