cryptoSL2024/utils.py

27 lines
740 B
Python
Raw Normal View History

2024-04-13 21:28:33 +02:00
from Crypto.PublicKey import ECC
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Hash import SHA512
from Crypto.Random import get_random_bytes
from secrets import randbelow
FILE = 'exchange.bin'
prime = 0xffffffffffffffffffffffffffffffff000000000000000000000001
def generate_secret(c = 0):
# 'DER' format for byte output
return ECC.generate(curve='NIST P-224').export_key(format='DER')
def H(secret):
# secret should be bytearray[], pref. from generate_secret() function
salt = get_random_bytes(16)
key = PBKDF2(generate_secret(), salt, 64, count=1000000, hmac_hash_module=SHA512)
return key[32:] # first 32 bytes of generated key
def coin_toss():
x = randbelow(2 ** 64)
if x & 1:
return False
else:
return True