summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Credential/SshKeygen.py
diff options
context:
space:
mode:
authorpikusa <pikusa@man.poznan.pl>2013-04-03 13:18:17 (GMT)
committer pikusa <pikusa@man.poznan.pl>2013-04-03 13:18:17 (GMT)
commit2f2a3a129c91de540e66c3bfbe30b0df1942cd4b (patch)
tree2d313cdf0068af368d4de6067d676be16f6a6464 /Monitoring/MonitoringService/Credential/SshKeygen.py
parentff8aa232b071a9b54dff833714a870fd0aec0b30 (diff)
downloadnovi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.zip
novi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.tar.gz
novi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.tar.bz2
project commit and dir tree change
Diffstat (limited to 'Monitoring/MonitoringService/Credential/SshKeygen.py')
-rw-r--r--Monitoring/MonitoringService/Credential/SshKeygen.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/Monitoring/MonitoringService/Credential/SshKeygen.py b/Monitoring/MonitoringService/Credential/SshKeygen.py
new file mode 100644
index 0000000..6c8042d
--- /dev/null
+++ b/Monitoring/MonitoringService/Credential/SshKeygen.py
@@ -0,0 +1,100 @@
+'''
+Created on Jul 20, 2011
+
+@author: steger
+'''
+from M2Crypto import RSA
+from base64 import b64encode
+from os import chmod, path
+import stat
+
+# paramiko provides this functionality, so maybe we don't need this class. see paramiko.PKey
+
+class CannotSet(Exception):
+ pass
+
+class SshKeygen(object):
+ '''
+ Generates a pair of RSA keys.
+ Enables saving the keys to the file system.
+ '''
+ def __init__(self, bits = 1024, e = 65337):
+ '''
+ Initiates the pair of RSA keys
+ @param bits: the length of the keys in bits
+ @type bits: integer
+ @param e: the exponent
+ @type e: integer
+ '''
+ self.rsa = RSA.gen_key(bits, e, lambda: None)
+
+ def _check_filename(self, filename):
+ if path.exists(filename):
+ raise Exception("File exists: %s" % filename)
+
+ @property
+ def private(self):
+ '''
+ @summary: return the private key in PEM format
+ @return: the private key in PEM format
+ @rtype: string
+ '''
+ return self.rsa.as_pem(cipher = None)
+
+ @private.setter
+ def private(self, value):
+ raise CannotSet
+
+ @private.deleter
+ def private(self):
+ raise CannotSet
+
+ @staticmethod
+ def _convert(rsa):
+ return b64encode('\x00\x00\x00\x07ssh-rsa%s%s' % (rsa.pub()[0], rsa.pub()[1]))
+
+ @property
+ def public(self):
+ '''
+ @summary: return the public key in base64 format conforming to the content of authorized_keys
+ @return: the public key in base64 format
+ @rtype: string
+ '''
+ return self._convert(self.rsa)
+
+ @public.setter
+ def public(self, value):
+ raise CannotSet
+
+ @public.deleter
+ def public(self):
+ raise CannotSet
+
+ def save_private_key(self, filename):
+ '''
+ @summary: save the private key in the file system in a named file.
+ @param filename: the filename to store the private key.
+ @type filename: string
+ '''
+ self._check_filename(filename)
+ self.rsa.save_key(filename, cipher = None)
+ chmod(filename, stat.S_IRUSR)
+
+ def save_public_key(self, filename):
+ '''
+ @summary: save the public key in the file system in a named file.
+ @param filename: the filename to store the public key.
+ @type filename: string
+ '''
+ self._check_filename(filename)
+ with open(filename, "w") as f:
+ f.write("ssh-rsa %s" % self.public)
+
+ @staticmethod
+ def convert_key_from_file(filename):
+ '''
+ @summary: convert a private key stored in a file in PEM format and return the public key in base64 format conforming to the content of authorized_keys
+ @return: the public key in base64 format
+ @rtype: string
+ '''
+ return SshKeygen._convert( RSA.load_key(file = filename) )