diff --git a/Pipfile b/Pipfile index b723d01..e10f309 100644 --- a/Pipfile +++ b/Pipfile @@ -4,8 +4,11 @@ url = "https://pypi.org/simple" verify_ssl = true [dev-packages] +pycrypto = "*" [packages] +pycrypto = "*" +fernet = "*" [requires] python_version = "3.7" diff --git a/cfg/config.json b/cfg/config.json index edb23df..08cafd9 100644 --- a/cfg/config.json +++ b/cfg/config.json @@ -1,3 +1,4 @@ { - "keyfile":"keyfile.json" + "pwdfile":"pwdfile.json", + "secret": null } \ No newline at end of file diff --git a/configuration.py b/configuration.py index c75419a..0295b15 100644 --- a/configuration.py +++ b/configuration.py @@ -1,10 +1,28 @@ import json -class Configuration: +class Config: def __init__(self): - with open('cfg/config.json') as cfgfile: - self.data = json.load(cfgfile) + self.data = self.read() - def get_keyfilename(self): - return self.data["keyfile"] + def get_pwdfilename(self): + return self.data["pwdfile"] + + def set_pwdfilename(self): + pass + + def get_secret(self): + return self.data["secret"] + + def set_secret(self, secret): + pass + + @staticmethod + def read(): + with open('cfg/config.json', mode="r") as cfgfile: + return json.load(cfgfile) + + @staticmethod + def write(keys): + with open('cfg/config.json', mode="w") as cfgfile: + cfgfile.write(json.dumps(keys)) diff --git a/credentials.py b/credentials.py index b446959..108c129 100644 --- a/credentials.py +++ b/credentials.py @@ -1,49 +1,39 @@ -import hashlib -import json -import uuid -from random import randint -from secrets import choice -from string import ascii_letters, digits - -from configuration import Configuration +from pwdfile import Pwdfile class Credentials: def __init__(self): - with open(Configuration().get_keyfilename(), mode="r+") as keydata: - self.keys = json.load(keydata) + self.creds = Pwdfile().read() def get_keys(self): - return self.keys + return self.creds - def add_key(self, service, username, password): + def read_key(self, service): + return self.creds[service] - pass + def create_key(self, service, username, password): + new_entry = { + "username": username, + "password": password + } + self.creds[service] = new_entry + Pwdfile().write(self.creds) - def get_key_by_service(self, service): - return self.keys[service] + def update_key(self, service, username=None, password=None): + current_entry = self.creds[service] + if username is None and password is not None: + self.creds[service] = { + "username": current_entry["username"], + "password": password + } + elif username is not None and password is None: + self.creds[service] = { + "username": username, + "password": current_entry["password"] + } + Pwdfile().write(self.creds) - @staticmethod - def gen_password(mn=12, mx=64): - return [ - ''.join(choice(ascii_letters + digits) - for _ in range(randint(mn, mx))) - ] + def delete_key(self, service): + del self.creds[service] + Pwdfile().write(self.creds) - @staticmethod - def hash_password(password): - salt = uuid.uuid4().hex - return hashlib.sha512( - salt.encode() + password.encode()).hexdigest() + ':' + salt - - @staticmethod - def check_password(hashed_password, user_password): - password, salt = hashed_password.split(':') - return password == hashlib.sha512( - salt.encode() + user_password.encode()).hexdigest() - - @staticmethod - def dsa_encode(password): - hash_object = hashlib.new('DSA') - hash_object.update(password) - return hash_object.h diff --git a/keyfile.json b/keyfile.json deleted file mode 100644 index 7383443..0000000 --- a/keyfile.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "twitter": { - "username": "@alwaysexiting", - "password": "some-password" - }, - "youtube": { - "username": "exitingthecave@gmail.com", - "password": "somepassword" - } -} diff --git a/password.py b/password.py new file mode 100644 index 0000000..a5122cf --- /dev/null +++ b/password.py @@ -0,0 +1,47 @@ +import hashlib +import uuid +from random import randint +from secrets import choice +from string import ascii_letters, digits +from cryptography.fernet import Fernet + +from configuration import Config + + +class Password: + def __init__(self): + self.encryption_key = Config().get_secret() + + def get_encryption_key(self): + pass + + @staticmethod + def generate(mn=16, mx=64): + return ''.join( + choice(ascii_letters + digits) for _ in range(randint(mn, mx))) + + @staticmethod + def encrypt(plain_password): + pass + + @staticmethod + def decrypt(encrypted_password): + pass + + @staticmethod + def hash_password(password): + salt = uuid.uuid4().hex + return hashlib.sha512( + salt.encode() + password.encode()).hexdigest() + ':' + salt + + @staticmethod + def check_password(hashed_password, user_password): + password, salt = hashed_password.split(':') + return password == hashlib.sha512( + salt.encode() + user_password.encode()).hexdigest() + + @staticmethod + def dsa_encode(password): + hash_object = hashlib.new('DSA') + hash_object.update(password) + return hash_object.h diff --git a/pwdfile.json b/pwdfile.json new file mode 100644 index 0000000..e7a7a17 --- /dev/null +++ b/pwdfile.json @@ -0,0 +1 @@ +{"twitter": {"username": "@alwaysexiting", "password": "gobbledygook"}, "youtube": {"username": "exitingthecave@gmail.com", "password": "somepassword"}} \ No newline at end of file diff --git a/pwdfile.py b/pwdfile.py new file mode 100644 index 0000000..59e1333 --- /dev/null +++ b/pwdfile.py @@ -0,0 +1,16 @@ +from configuration import Config +import json + + +class Pwdfile: + def __init__(self): + self.keysfile = Config().get_pwdfilename() + + def read(self): + with open(self.keysfile, mode="r") as keydata: + return json.load(keydata) + + def write(self, keys): + with open(self.keysfile, mode="w") as keydata: + keydata.write(json.dumps(keys)) + diff --git a/testing.txt b/testing.txt deleted file mode 100644 index be953a8..0000000 --- a/testing.txt +++ /dev/null @@ -1,4 +0,0 @@ -line one -line two -line three -line fourline fiveline six \ No newline at end of file