|
|
@@ -0,0 +1,23 @@
|
|
|
+from cryptography.hazmat.backends import default_backend
|
|
|
+from cryptography.hazmat.primitives import serialization as crypt_serialization
|
|
|
+from cryptography.hazmat.primitives.asymmetric import rsa
|
|
|
+
|
|
|
+
|
|
|
+def generate_key_pair():
|
|
|
+ """
|
|
|
+ This method generates a keypair and returns it as a tuple
|
|
|
+ of (public, private) keys.
|
|
|
+ The public key format is OpenSSH and private key format is PEM.
|
|
|
+ """
|
|
|
+ key_pair = rsa.generate_private_key(
|
|
|
+ backend=default_backend(),
|
|
|
+ public_exponent=65537,
|
|
|
+ key_size=2048)
|
|
|
+ private_key = key_pair.private_bytes(
|
|
|
+ crypt_serialization.Encoding.PEM,
|
|
|
+ crypt_serialization.PrivateFormat.PKCS8,
|
|
|
+ crypt_serialization.NoEncryption()).decode('utf-8')
|
|
|
+ public_key = key_pair.public_key().public_bytes(
|
|
|
+ crypt_serialization.Encoding.OpenSSH,
|
|
|
+ crypt_serialization.PublicFormat.OpenSSH).decode('utf-8')
|
|
|
+ return public_key, private_key
|