Generate signing keys using python3-cryptography

merge-requests/30/head
Bob Mottram 2021-02-04 17:26:00 +00:00
parent 22b946257d
commit 3f248ce33b
1 changed files with 20 additions and 7 deletions

View File

@ -13,10 +13,9 @@ import shutil
import pyqrcode import pyqrcode
from random import randint from random import randint
from pathlib import Path from pathlib import Path
try: from cryptography.hazmat.backends import default_backend
from Cryptodome.PublicKey import RSA from cryptography.hazmat.primitives.asymmetric import rsa
except ImportError: from cryptography.hazmat.primitives import serialization
from Crypto.PublicKey import RSA
from shutil import copyfile from shutil import copyfile
from webfinger import createWebfingerEndpoint from webfinger import createWebfingerEndpoint
from webfinger import storeWebfingerEndpoint from webfinger import storeWebfingerEndpoint
@ -44,9 +43,23 @@ from utils import getConfigParam
def generateRSAKey() -> (str, str): def generateRSAKey() -> (str, str):
key = RSA.generate(2048) key = rsa.generate_private_key(
privateKeyPem = key.exportKey("PEM").decode("utf-8") public_exponent=65537,
publicKeyPem = key.publickey().exportKey("PEM").decode("utf-8") key_size=2048,
backend=default_backend()
)
privateKeyPem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
pubkey = key.public_key()
publicKeyPem = pubkey.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
privateKeyPem = privateKeyPem.decode("utf-8")
publicKeyPem = publicKeyPem.decode("utf-8")
return privateKeyPem, publicKeyPem return privateKeyPem, publicKeyPem