Hexa format
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DESKTOP-DPA61F8\Benedek 2021-04-21 18:30:18 +02:00
parent a2e09b3656
commit be21d4100d

View File

@ -3,6 +3,7 @@ import logging
import os import os
import shutil import shutil
import sys import sys
import binascii
from base64 import b64encode from base64 import b64encode
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256 from Crypto.Hash import SHA256
@ -19,6 +20,7 @@ class Authetication:
PRIVATE_KEY_DIRECTORY_LOCATION = CONFIG_DIRECTORY_LOCATION + os.path.sep + "private_keys" PRIVATE_KEY_DIRECTORY_LOCATION = CONFIG_DIRECTORY_LOCATION + os.path.sep + "private_keys"
USER_INDEX = 0 USER_INDEX = 0
def __init__(self): def __init__(self):
if not os.path.isdir(self.HOME_DIRECTORY_LOCATION): if not os.path.isdir(self.HOME_DIRECTORY_LOCATION):
os.mkdir(self.HOME_DIRECTORY_LOCATION) os.mkdir(self.HOME_DIRECTORY_LOCATION)
@ -35,6 +37,7 @@ class Authetication:
with open(self.CONFIG_FILE_LOCATION, 'w+') as outfile: with open(self.CONFIG_FILE_LOCATION, 'w+') as outfile:
json.dump(data, outfile) json.dump(data, outfile)
def login(self, username: str, password: str) -> str: def login(self, username: str, password: str) -> str:
with open(self.CONFIG_FILE_LOCATION) as json_file: with open(self.CONFIG_FILE_LOCATION) as json_file:
data = json.load(json_file) data = json.load(json_file)
@ -51,6 +54,7 @@ class Authetication:
auth_logger.debug("User logged in: " + username) auth_logger.debug("User logged in: " + username)
return user['homeDir'] return user['homeDir']
def checkUserExists(self, username: str) -> bool: def checkUserExists(self, username: str) -> bool:
with open(self.CONFIG_FILE_LOCATION) as json_file: with open(self.CONFIG_FILE_LOCATION) as json_file:
data = json.load(json_file) data = json.load(json_file)
@ -60,6 +64,7 @@ class Authetication:
return True return True
return False return False
def initConfig(self): def initConfig(self):
data = {'index': 0, 'user': []} data = {'index': 0, 'user': []}
with open(self.CONFIG_FILE_LOCATION, 'w+') as outfile: with open(self.CONFIG_FILE_LOCATION, 'w+') as outfile:
@ -78,13 +83,15 @@ class Authetication:
private_key = RSA.generate(2048) private_key = RSA.generate(2048)
public_key = private_key.publickey() public_key = private_key.publickey()
private_key_value = str(private_key.export_key('DER', passphrase=user_passphrase, pkcs=8)) private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=user_passphrase, pkcs=8))
public_key_value = str(public_key.export_key('DER', pkcs=8)) public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8))
##Save private key in separate file ##Save private key in separate file
user_privatekey = {'passphrase': user_passphrase, 'privateClientKey': private_key_value, 'publicServerKey': public_server_key} user_privatekey = {'passphrase': user_passphrase, 'privateClientKey': private_key_value, 'publicServerKey': public_server_key}
with open(self.PRIVATE_KEY_DIRECTORY_LOCATION + os.path.sep + str(data['index']) + '.txt', 'w+') as outfile: with open(self.PRIVATE_KEY_DIRECTORY_LOCATION + os.path.sep + str(data['index']) + '.txt', 'w+') as outfile:
json.dump(user_privatekey, outfile) json.dump(user_privatekey, outfile)
outfile.close()
##Save public key in users ##Save public key in users
for user in data['user']: for user in data['user']:
@ -93,11 +100,15 @@ class Authetication:
with open(self.CONFIG_FILE_LOCATION, 'w') as outfile: with open(self.CONFIG_FILE_LOCATION, 'w') as outfile:
json.dump(data, outfile) json.dump(data, outfile)
break break
outfile.close()
return True return True
else: else:
return False return False
def saveUser(self, username: str, password: str) -> bool: def saveUser(self, username: str, password: str) -> bool:
bytePass = password.encode('utf-8') bytePass = password.encode('utf-8')
b64pwd = b64encode(SHA256.new(bytePass).digest()) b64pwd = b64encode(SHA256.new(bytePass).digest())