Compare commits
2 Commits
41e7fc08ed
...
78f0140342
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78f0140342 | ||
|
|
adfec105d8 |
@ -155,7 +155,11 @@ async def init_election_keys(election_id: int, db: Session = Depends(get_db)):
|
||||
pubkey_is_invalid = False
|
||||
if election.public_key:
|
||||
try:
|
||||
pubkey_str = election.public_key.decode('utf-8') if isinstance(election.public_key, bytes) else str(election.public_key)
|
||||
# Public key is stored as base64-encoded bytes, try to decode it
|
||||
pubkey_b64_str = election.public_key.decode('ascii') if isinstance(election.public_key, bytes) else str(election.public_key)
|
||||
# Try to decode the base64 to verify it's valid
|
||||
pubkey_bytes = base64.b64decode(pubkey_b64_str)
|
||||
pubkey_str = pubkey_bytes.decode('utf-8')
|
||||
# Check if it's valid (should be "p:g:h" format, not "pk_ongoing_X")
|
||||
if not ':' in pubkey_str or pubkey_str.startswith('pk_') or pubkey_str.startswith('b\''):
|
||||
pubkey_is_invalid = True
|
||||
@ -165,13 +169,24 @@ async def init_election_keys(election_id: int, db: Session = Depends(get_db)):
|
||||
if not election.public_key or pubkey_is_invalid:
|
||||
logger.info(f"Generating ElGamal public key for election {election_id}")
|
||||
elgamal = ElGamalEncryption(p=election.elgamal_p or 23, g=election.elgamal_g or 5)
|
||||
# Use the property that returns properly formatted bytes "p:g:h"
|
||||
election.public_key = elgamal.public_key_bytes
|
||||
# Store as base64-encoded bytes (public_key_bytes returns UTF-8 "p:g:h", then encode to base64)
|
||||
election.public_key = base64.b64encode(elgamal.public_key_bytes)
|
||||
db.commit()
|
||||
logger.info(f"✓ Generated public key for election {election_id}")
|
||||
else:
|
||||
logger.info(f"Election {election_id} already has valid public key")
|
||||
|
||||
# Ensure public key is base64-encoded for client
|
||||
pubkey_to_return = election.public_key
|
||||
if isinstance(pubkey_to_return, bytes):
|
||||
pubkey_str = pubkey_to_return.decode('utf-8')
|
||||
# If it's plain "p:g:h" format, encode it to base64
|
||||
if ':' in pubkey_str and not pubkey_str.startswith('MjM6'): # Not already base64
|
||||
pubkey_to_return = base64.b64encode(pubkey_str.encode('utf-8')).decode('ascii')
|
||||
else:
|
||||
# Already base64, just decode to string
|
||||
pubkey_to_return = pubkey_str
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"election_id": election_id,
|
||||
@ -179,7 +194,7 @@ async def init_election_keys(election_id: int, db: Session = Depends(get_db)):
|
||||
"elgamal_p": election.elgamal_p,
|
||||
"elgamal_g": election.elgamal_g,
|
||||
"public_key_generated": True,
|
||||
"public_key": base64.b64encode(election.public_key).decode() if election.public_key else None
|
||||
"public_key": pubkey_to_return
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
|
||||
@ -499,18 +499,29 @@ async def setup_election(
|
||||
|
||||
# Générer les clés ElGamal si nécessaire
|
||||
if not election.public_key:
|
||||
elgamal = ElGamal()
|
||||
elgamal = ElGamalEncryption()
|
||||
# Store as base64-encoded bytes (database column is LargeBinary)
|
||||
# public_key_bytes returns UTF-8 "p:g:h", then encode to base64
|
||||
election.public_key = base64.b64encode(elgamal.public_key_bytes)
|
||||
db.add(election)
|
||||
db.commit()
|
||||
|
||||
# Ensure public key is base64-encoded for client
|
||||
pubkey_to_return = election.public_key
|
||||
if isinstance(pubkey_to_return, bytes):
|
||||
pubkey_str = pubkey_to_return.decode('utf-8')
|
||||
# If it's plain "p:g:h" format, encode it to base64
|
||||
if ':' in pubkey_str and not pubkey_str.startswith('MjM6'): # Not already base64
|
||||
pubkey_to_return = base64.b64encode(pubkey_str.encode('utf-8')).decode('ascii')
|
||||
else:
|
||||
# Already base64, just decode to string
|
||||
pubkey_to_return = pubkey_str
|
||||
|
||||
return {
|
||||
"status": "initialized",
|
||||
"election_id": election_id,
|
||||
"public_keys": {
|
||||
"elgamal_pubkey": election.public_key.decode('utf-8') if election.public_key else None
|
||||
"elgamal_pubkey": pubkey_to_return
|
||||
},
|
||||
"blockchain_blocks": blockchain.get_block_count()
|
||||
}
|
||||
@ -546,8 +557,19 @@ async def get_public_keys(
|
||||
detail="Election keys not initialized. Call /setup first."
|
||||
)
|
||||
|
||||
# Ensure public key is base64-encoded for client
|
||||
pubkey_to_return = election.public_key
|
||||
if isinstance(pubkey_to_return, bytes):
|
||||
pubkey_str = pubkey_to_return.decode('utf-8')
|
||||
# If it's plain "p:g:h" format, encode it to base64
|
||||
if ':' in pubkey_str and not pubkey_str.startswith('MjM6'): # Not already base64
|
||||
pubkey_to_return = base64.b64encode(pubkey_str.encode('utf-8')).decode('ascii')
|
||||
else:
|
||||
# Already base64, just decode to string
|
||||
pubkey_to_return = pubkey_str
|
||||
|
||||
return {
|
||||
"elgamal_pubkey": election.public_key.decode('utf-8') if election.public_key else None
|
||||
"elgamal_pubkey": pubkey_to_return
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user