32 lines
995 B
Python
32 lines
995 B
Python
import os
|
|
import sys
|
|
|
|
import M2Crypto
|
|
|
|
import config
|
|
import log
|
|
|
|
c = config.Config()
|
|
logger = log.initialize_logging("fail2ban-p2p." + __name__)
|
|
|
|
|
|
def create_keys():
|
|
"""Create private/public keypair (RSA 1024 bit)."""
|
|
if os.path.isfile(c.privkey) or os.path.isfile(c.pubkey):
|
|
print("A keypair for this node already exists.")
|
|
ask = input('Do you really want to create a new one? [y/N] ')
|
|
if ask != "y":
|
|
return
|
|
M2Crypto.Rand.rand_seed(os.urandom(1024))
|
|
logger.info("Generating a 1024 bit private/public key pair...")
|
|
keypair = M2Crypto.RSA.gen_key(1024, 65537)
|
|
try:
|
|
keypair.save_key(c.privkey, None)
|
|
os.chmod(c.privkey, 0o400)
|
|
keypair.save_pub_key(c.pubkey)
|
|
logger.debug("Private key (secret) was saved to %s", c.privkey)
|
|
logger.debug("Public key was saved to %s", c.pubkey)
|
|
except IOError as e:
|
|
logger.error("Could not save the keypair, check permissions! %s", e)
|
|
sys.exit(1)
|