get the password encryption tools working

This commit is contained in:
Greg Gauthier 2019-03-12 14:57:58 +00:00
parent 5bfabb48db
commit 64149b54fc
5 changed files with 52 additions and 41 deletions

View File

@ -1,4 +1 @@
{ {"pwdfilename": "pwdfile.json", "secret": null}
"pwdfile":"pwdfile.json",
"secret": null
}

View File

@ -8,14 +8,16 @@ class Config:
def get_pwdfilename(self): def get_pwdfilename(self):
return self.data["pwdfile"] return self.data["pwdfile"]
def set_pwdfilename(self): def set_pwdfilename(self, pwdfilename):
pass self.data["pwdfilename"] = pwdfilename
self.write(self.data)
def get_secret(self): def get_secret(self):
return self.data["secret"] return self.data["secret"]
def set_secret(self, secret): def set_secret(self, secret):
pass self.data["secret"] = secret
self.write(self.data)
@staticmethod @staticmethod
def read(): def read():

View File

@ -5,13 +5,13 @@ class Credentials:
def __init__(self): def __init__(self):
self.creds = Pwdfile().read() self.creds = Pwdfile().read()
def get_keys(self): def get_creds(self):
return self.creds return self.creds
def read_key(self, service): def read_cred(self, service):
return self.creds[service] return self.creds[service]
def create_key(self, service, username, password): def create_cred(self, service, username, password):
new_entry = { new_entry = {
"username": username, "username": username,
"password": password "password": password
@ -19,7 +19,7 @@ class Credentials:
self.creds[service] = new_entry self.creds[service] = new_entry
Pwdfile().write(self.creds) 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] current_entry = self.creds[service]
if username is None and password is not None: if username is None and password is not None:
self.creds[service] = { self.creds[service] = {
@ -33,7 +33,7 @@ class Credentials:
} }
Pwdfile().write(self.creds) Pwdfile().write(self.creds)
def delete_key(self, service): def delete_cred(self, service):
del self.creds[service] del self.creds[service]
Pwdfile().write(self.creds) Pwdfile().write(self.creds)

View File

@ -3,7 +3,7 @@ import uuid
from random import randint from random import randint
from secrets import choice from secrets import choice
from string import ascii_letters, digits from string import ascii_letters, digits
from cryptography.fernet import Fernet from cryptography.fernet import Fernet, InvalidToken
from configuration import Config from configuration import Config
@ -11,37 +11,49 @@ from configuration import Config
class Password: class Password:
def __init__(self): def __init__(self):
self.encryption_key = Config().get_secret() 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): 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 @staticmethod
def generate(mn=16, mx=64): def generate(mn=16, mx=64):
return ''.join( return ''.join(
choice(ascii_letters + digits) for _ in range(randint(mn, mx))) choice(ascii_letters + digits) for _ in range(randint(mn, mx))
)
@staticmethod # @staticmethod
def encrypt(plain_password): # def hash_password(password):
pass # salt = uuid.uuid4().hex
# return hashlib.sha512(
@staticmethod # salt.encode() + password.encode()).hexdigest() + ':' + salt
def decrypt(encrypted_password): #
pass # @staticmethod
# def check_password(hashed_password, user_password):
@staticmethod # password, salt = hashed_password.split(':')
def hash_password(password): # return password == hashlib.sha512(
salt = uuid.uuid4().hex # salt.encode() + user_password.encode()).hexdigest()
return hashlib.sha512( #
salt.encode() + password.encode()).hexdigest() + ':' + salt # @staticmethod
# def dsa_encode(password):
@staticmethod # hash_object = hashlib.new('DSA')
def check_password(hashed_password, user_password): # hash_object.update(password)
password, salt = hashed_password.split(':') # return hash_object.h
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

View File

@ -1 +1 @@
{"twitter": {"username": "@alwaysexiting", "password": "gobbledygook"}, "youtube": {"username": "exitingthecave@gmail.com", "password": "somepassword"}} {"twitter": {"username": "@twitterhandle", "password": "gobbledygook"}, "youtube": {"username": "googlemail@gmail.com", "password": "moregobbledygook"}}