diff --git a/cfg/config.json b/cfg/config.json index 08cafd9..381e6c1 100644 --- a/cfg/config.json +++ b/cfg/config.json @@ -1,4 +1 @@ -{ - "pwdfile":"pwdfile.json", - "secret": null -} \ No newline at end of file +{"pwdfilename": "pwdfile.json", "secret": null} \ No newline at end of file diff --git a/configuration.py b/configuration.py index 0295b15..4161304 100644 --- a/configuration.py +++ b/configuration.py @@ -8,14 +8,16 @@ class Config: def get_pwdfilename(self): return self.data["pwdfile"] - def set_pwdfilename(self): - pass + def set_pwdfilename(self, pwdfilename): + self.data["pwdfilename"] = pwdfilename + self.write(self.data) def get_secret(self): return self.data["secret"] def set_secret(self, secret): - pass + self.data["secret"] = secret + self.write(self.data) @staticmethod def read(): diff --git a/credentials.py b/credentials.py index 108c129..0297bb9 100644 --- a/credentials.py +++ b/credentials.py @@ -5,13 +5,13 @@ class Credentials: def __init__(self): self.creds = Pwdfile().read() - def get_keys(self): + def get_creds(self): return self.creds - def read_key(self, service): + def read_cred(self, service): return self.creds[service] - def create_key(self, service, username, password): + def create_cred(self, service, username, password): new_entry = { "username": username, "password": password @@ -19,7 +19,7 @@ class Credentials: self.creds[service] = new_entry Pwdfile().write(self.creds) - def update_key(self, service, username=None, password=None): + def update_cred(self, service, username=None, password=None): current_entry = self.creds[service] if username is None and password is not None: self.creds[service] = { @@ -33,7 +33,7 @@ class Credentials: } Pwdfile().write(self.creds) - def delete_key(self, service): + def delete_cred(self, service): del self.creds[service] Pwdfile().write(self.creds) diff --git a/password.py b/password.py index a5122cf..3568872 100644 --- a/password.py +++ b/password.py @@ -3,7 +3,7 @@ import uuid from random import randint from secrets import choice from string import ascii_letters, digits -from cryptography.fernet import Fernet +from cryptography.fernet import Fernet, InvalidToken from configuration import Config @@ -11,37 +11,49 @@ from configuration import Config class Password: def __init__(self): self.encryption_key = Config().get_secret() + if self.encryption_key is None: + self.set_encryption_key() + else: # just take what's given + self.cipher = Fernet(self.encryption_key) def get_encryption_key(self): - pass + return self.encryption_key + + def set_encryption_key(self): + self.encryption_key = Fernet.generate_key() + Config().set_secret(self.encryption_key.decode()) # store as string + # Don't forget to update the cipher!!! + self.cipher = Fernet(self.encryption_key) + + def encrypt(self, plain_password): + return self.cipher.encrypt(plain_password.encode()) + + def decrypt(self, encrypted_password): + try: + return self.cipher.decrypt(encrypted_password).decode() + except InvalidToken: + return "ERROR: Invalid Encryption Key" @staticmethod def generate(mn=16, mx=64): return ''.join( - choice(ascii_letters + digits) for _ in range(randint(mn, mx))) + 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 + # @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 index e7a7a17..f603776 100644 --- a/pwdfile.json +++ b/pwdfile.json @@ -1 +1 @@ -{"twitter": {"username": "@alwaysexiting", "password": "gobbledygook"}, "youtube": {"username": "exitingthecave@gmail.com", "password": "somepassword"}} \ No newline at end of file +{"twitter": {"username": "@twitterhandle", "password": "gobbledygook"}, "youtube": {"username": "googlemail@gmail.com", "password": "moregobbledygook"}} \ No newline at end of file