示例#1
0
def generate_key_pair(key_length=2048):
    """Create RSA key pair with specified number of bits in key.

    Returns tuple of private and public keys.
    """
    with tempfiles.tempdir() as tmpdir:
        keyfile = os.path.join(tmpdir, 'tempkey')
        # The key is generated in the old PEM format, instead of the native
        # format of OpenSSH >=6.5, because paramiko does not support it:
        # https://github.com/paramiko/paramiko/issues/602
        args = [
            'ssh-keygen',
            '-q',  # quiet
            '-N', '',  # w/o passphrase
            '-m', 'PEM',  # old PEM format
            '-t', 'rsa',  # create key of rsa type
            '-f', keyfile,  # filename of the key file
            '-C', 'Generated-by-Sahara'  # key comment
        ]
        if key_length is not None:
            args.extend(['-b', key_length])
        processutils.execute(*args)
        if not os.path.exists(keyfile):
            raise ex.SystemError(_("Private key file hasn't been created"))
        with open(keyfile) as keyfile_fd:
            private_key = keyfile_fd.read()
        public_key_path = keyfile + '.pub'
        if not os.path.exists(public_key_path):
            raise ex.SystemError(_("Public key file hasn't been created"))
        with open(public_key_path) as public_key_path_fd:
            public_key = public_key_path_fd.read()

        return private_key, public_key
示例#2
0
文件: crypto.py 项目: savi-dev/sahara
def generate_key_pair(key_length=2048):
    """Create RSA key pair with specified number of bits in key.

    Returns tuple of private and public keys.
    """
    with tempfiles.tempdir() as tmpdir:
        keyfile = os.path.join(tmpdir, 'tempkey')
        args = [
            'ssh-keygen',
            '-q',  # quiet
            '-N', '',  # w/o passphrase
            '-t', 'rsa',  # create key of rsa type
            '-f', keyfile,  # filename of the key file
            '-C', 'Generated by Sahara'  # key comment
        ]
        if key_length is not None:
            args.extend(['-b', key_length])
        processutils.execute(*args)
        if not os.path.exists(keyfile):
            raise ex.SystemError("Private key file hasn't been created")
        private_key = open(keyfile).read()
        public_key_path = keyfile + '.pub'
        if not os.path.exists(public_key_path):
            raise ex.SystemError("Public key file hasn't been created")
        public_key = open(public_key_path).read()

        return private_key, public_key
示例#3
0
def generate_key_pair(key_length=2048):
    """Create RSA key pair with specified number of bits in key.

    Returns tuple of private and public keys.
    """
    with tempfiles.tempdir() as tmpdir:
        keyfile = os.path.join(tmpdir, "tempkey")
        args = [
            "ssh-keygen",
            "-q",  # quiet
            "-N",
            "",  # w/o passphrase
            "-t",
            "rsa",  # create key of rsa type
            "-f",
            keyfile,  # filename of the key file
            "-C",
            "Generated-by-Sahara",  # key comment
        ]
        if key_length is not None:
            args.extend(["-b", key_length])
        processutils.execute(*args)
        if not os.path.exists(keyfile):
            raise ex.SystemError(_("Private key file hasn't been created"))
        with open(keyfile) as keyfile_fd:
            private_key = keyfile_fd.read()
        public_key_path = keyfile + ".pub"
        if not os.path.exists(public_key_path):
            raise ex.SystemError(_("Public key file hasn't been created"))
        with open(public_key_path) as public_key_path_fd:
            public_key = public_key_path_fd.read()

        return private_key, public_key