Ver código fonte

Added file omitted during previous commit

Nuwan Goonasekera 8 anos atrás
pai
commit
f20c4ea145
1 arquivos alterados com 23 adições e 0 exclusões
  1. 23 0
      cloudbridge/cloud/base/helpers.py

+ 23 - 0
cloudbridge/cloud/base/helpers.py

@@ -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