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 @@
{
"pwdfile":"pwdfile.json",
"secret": null
}
{"pwdfilename": "pwdfile.json", "secret": null}

View File

@ -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():

View File

@ -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)

View File

@ -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

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"}}