mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 06:04:21 +01:00
Switched to cryptography instead of ecdsa for signatures
This commit is contained in:
@@ -1,21 +1,47 @@
|
||||
import ecdsa
|
||||
import os.path
|
||||
from sys import exit
|
||||
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
|
||||
|
||||
# Simple tool to generate an ECDSA private key using the secp256k1 curve and save private and public keys
|
||||
# as signing_key_priv.pem and signing_key_pub.pem
|
||||
|
||||
FILE_NAME_PRIV = 'signing_key_priv.pem'
|
||||
FILE_NAME_PUB = 'signing_key_pub.pem'
|
||||
|
||||
|
||||
def save_sk(sk, filename):
|
||||
pem = sk.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption()
|
||||
)
|
||||
with open(filename, 'wb') as pem_out:
|
||||
pem_out.write(pem)
|
||||
|
||||
|
||||
def save_pk(pk, filename):
|
||||
pem = pk.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
)
|
||||
with open(filename, 'wb') as pem_out:
|
||||
pem_out.write(pem)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if os.path.exists(FILE_NAME_PRIV):
|
||||
print("A key with name \"{}\" already exists. Aborting.".format(FILE_NAME_PRIV))
|
||||
exit(1)
|
||||
|
||||
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
|
||||
pk = sk.get_verifying_key()
|
||||
sk = ec.generate_private_key(
|
||||
ec.SECP256K1, default_backend()
|
||||
)
|
||||
pk = sk.public_key()
|
||||
|
||||
open(FILE_NAME_PRIV, 'wb').write(sk.to_pem())
|
||||
open(FILE_NAME_PUB, 'wb').write(pk.to_pem())
|
||||
save_sk(sk, FILE_NAME_PRIV)
|
||||
save_pk(pk, FILE_NAME_PUB)
|
||||
print("Saved private key \"{}\" and public key \"{}\".".format(FILE_NAME_PRIV, FILE_NAME_PUB))
|
||||
|
||||
Reference in New Issue
Block a user