refactor
This commit is contained in:
parent
9e16efd4cd
commit
7650571ad0
3
Pipfile
3
Pipfile
@ -4,8 +4,11 @@ url = "https://pypi.org/simple"
|
|||||||
verify_ssl = true
|
verify_ssl = true
|
||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
|
pycrypto = "*"
|
||||||
|
|
||||||
[packages]
|
[packages]
|
||||||
|
pycrypto = "*"
|
||||||
|
fernet = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.7"
|
python_version = "3.7"
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"keyfile":"keyfile.json"
|
"pwdfile":"pwdfile.json",
|
||||||
|
"secret": null
|
||||||
}
|
}
|
@ -1,10 +1,28 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class Configuration:
|
class Config:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
with open('cfg/config.json') as cfgfile:
|
self.data = self.read()
|
||||||
self.data = json.load(cfgfile)
|
|
||||||
|
|
||||||
def get_keyfilename(self):
|
def get_pwdfilename(self):
|
||||||
return self.data["keyfile"]
|
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))
|
||||||
|
@ -1,49 +1,39 @@
|
|||||||
import hashlib
|
from pwdfile import Pwdfile
|
||||||
import json
|
|
||||||
import uuid
|
|
||||||
from random import randint
|
|
||||||
from secrets import choice
|
|
||||||
from string import ascii_letters, digits
|
|
||||||
|
|
||||||
from configuration import Configuration
|
|
||||||
|
|
||||||
|
|
||||||
class Credentials:
|
class Credentials:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
with open(Configuration().get_keyfilename(), mode="r+") as keydata:
|
self.creds = Pwdfile().read()
|
||||||
self.keys = json.load(keydata)
|
|
||||||
|
|
||||||
def get_keys(self):
|
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):
|
def update_key(self, service, username=None, password=None):
|
||||||
return self.keys[service]
|
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 delete_key(self, service):
|
||||||
def gen_password(mn=12, mx=64):
|
del self.creds[service]
|
||||||
return [
|
Pwdfile().write(self.creds)
|
||||||
''.join(choice(ascii_letters + digits)
|
|
||||||
for _ in range(randint(mn, mx)))
|
|
||||||
]
|
|
||||||
|
|
||||||
@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
|
|
||||||
|
10
keyfile.json
10
keyfile.json
@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"twitter": {
|
|
||||||
"username": "@alwaysexiting",
|
|
||||||
"password": "some-password"
|
|
||||||
},
|
|
||||||
"youtube": {
|
|
||||||
"username": "exitingthecave@gmail.com",
|
|
||||||
"password": "somepassword"
|
|
||||||
}
|
|
||||||
}
|
|
47
password.py
Normal file
47
password.py
Normal 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
1
pwdfile.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"twitter": {"username": "@alwaysexiting", "password": "gobbledygook"}, "youtube": {"username": "exitingthecave@gmail.com", "password": "somepassword"}}
|
16
pwdfile.py
Normal file
16
pwdfile.py
Normal 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))
|
||||||
|
|
@ -1,4 +0,0 @@
|
|||||||
line one
|
|
||||||
line two
|
|
||||||
line three
|
|
||||||
line fourline fiveline six
|
|
Loading…
Reference in New Issue
Block a user