First commit
This commit is contained in:
62
src/collection.py
Normal file
62
src/collection.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from pathlib import Path
|
||||
from os import walk, chmod
|
||||
from Crypto.PublicKey import RSA
|
||||
|
||||
class ssh_key:
|
||||
"""
|
||||
Object class for a ssh key
|
||||
"""
|
||||
def __init__(self, name, key_type, public, private):
|
||||
self.name = name
|
||||
self.type = key_type
|
||||
self.public = public
|
||||
self.private = private
|
||||
|
||||
def get_name(self):
|
||||
"""Getter for name"""
|
||||
return self.name
|
||||
def get_type(self):
|
||||
"""Getter for type"""
|
||||
return self.type
|
||||
def get_private(self):
|
||||
"""Getter for private key"""
|
||||
return self.private
|
||||
def get_public(self):
|
||||
"""Getter for public key"""
|
||||
return self.public
|
||||
|
||||
class Collection:
|
||||
"""
|
||||
Object class of Collection type
|
||||
"""
|
||||
def __init__(self, collection_name):
|
||||
self.collection_name = collection_name
|
||||
self.collection_path = Path.home().joinpath(".sshkeymanager", self.collection_name)
|
||||
|
||||
def generate_ssh_key(self, name: str, key_type: str):
|
||||
"""
|
||||
public class to generate a ssh key
|
||||
"""
|
||||
key = RSA.generate(2048)
|
||||
sshKey = ssh_key(name=name, key_type=key_type, private=key.exportKey('PEM'), public=key.publickey().exportKey('OpenSSH'))
|
||||
self.save_ssh_key(sshKey=sshKey)
|
||||
|
||||
def save_ssh_key(self, sshKey: ssh_key):
|
||||
"""
|
||||
Function to save the sshkey
|
||||
"""
|
||||
key_file_path = self.collection_path.joinpath(sshKey.get_name())
|
||||
with open(f"{key_file_path}.txt", "w+", encoding="utf-8") as info_file:
|
||||
info_file.write(f"name: {sshKey.get_name()}\nKey_type: {sshKey.get_type()}")
|
||||
with open(key_file_path, "wb") as private_file:
|
||||
private_file.write(sshKey.get_private())
|
||||
with open(f"{key_file_path}.pub", "wb") as public_file:
|
||||
public_file.write(sshKey.get_public())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user