This commit is contained in:
Greg Gauthier 2019-03-12 12:27:34 +00:00
parent 9e16efd4cd
commit 7650571ad0
9 changed files with 120 additions and 58 deletions

View File

@ -4,8 +4,11 @@ url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
pycrypto = "*"
[packages]
pycrypto = "*"
fernet = "*"
[requires]
python_version = "3.7"

View File

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

View File

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

View File

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

View File

@ -1,10 +0,0 @@
{
"twitter": {
"username": "@alwaysexiting",
"password": "some-password"
},
"youtube": {
"username": "exitingthecave@gmail.com",
"password": "somepassword"
}
}

47
password.py Normal file
View File

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

1
pwdfile.json Normal file
View File

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

16
pwdfile.py Normal file
View File

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

View File

@ -1,4 +0,0 @@
line one
line two
line three
line fourline fiveline six