Add prep_keys function to the crypt module

This commit is contained in:
Thomas S Hatch 2011-02-20 23:01:41 -07:00
parent a70ff624af
commit 8a6fa97830

View file

@ -3,3 +3,30 @@ The crypt module manages all of the cyptogophy functions for minions and
masters, encrypting and decrypting payloads, preparing messages, and
authenticating peers
'''
# Import python libs
import os
# Import pycrypto libs
import Crypto.PublicKey.RSA as RSA
def prep_keys(keydir, name):
'''
Generate an rsa key pair and save it in the specified directory, return
the rsa object.
'''
rsa = None
if not os.path.exists(keydir):
os.makedirs(keydir)
key = os.path.join(keydir, name)
if os.path.isfile(key):
# The key exists, load it and return it
rsa = RSA.importKey(open(key, 'r').read())
if not os.path.isfile(key + '.pub'):
open(key + '.pub', 'w+').write(rsa.publickey().exportKey())
else:
# The key needs to be generated and saved
rsa = RSA.generate(1024)
open(key, 'w+').write(rsa.exportKey())
open(key + '.pub', 'w+').write(rsa.publickey().exportKey())
return rsa