Files
fail2ban-p2p/fail2ban-p2p/crypto.py
2026-04-23 01:55:12 +02:00

41 lines
1.2 KiB
Python

# Copyright 2013 Johannes Fuermann <johannes at fuermann.cc>
# Copyright 2013 Manuel Munz <manu at somakoma.de>
#
# This file is part of fail2ban-p2p.
#
# Licensed under the GNU GENERAL PUBLIC LICENSE Version 3. For details
# see the file COPYING or http://www.gnu.org/licenses/gpl-3.0.en.html.
import os
from M2Crypto import Rand, RSA
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
Rand.rand_seed(os.urandom(1024))
logger.info("Generating a 1024 bit private/public key pair...")
keypair = 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)
raise