server/server/config_init.py
DESKTOP-DPA61F8\Benedek 45bab9c9f3
All checks were successful
continuous-integration/drone/push Build is passing
Long key
2021-04-25 19:15:08 +02:00

73 lines
2.6 KiB
Python

#!/usr/bin/env python3
import json
import os
from Crypto.PublicKey import RSA
from authentication import Authetication
def generatePrivateKeyForUser(auth: Authetication,username: str, user_passphrase: str, public_server_key: str,address: str) -> bool:
if auth.checkUserExists(username):
with open(auth.CONFIG_FILE_LOCATION) as json_file:
data = json.load(json_file)
private_key = RSA.generate(8192)
public_key = private_key.publickey()
private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=user_passphrase, pkcs=8))
public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8))
##Save private key in separate file
user_privatekey = {'address': address,
'privateClientKey': private_key_value,
'publicServerKey': public_server_key}
with open(auth.PRIVATE_KEY_DIRECTORY_LOCATION + os.path.sep + str(data['index']) + '.txt',
'w+') as outfile:
json.dump(user_privatekey, outfile)
outfile.close()
##Save public key in users
for user in data['user']:
if username == user['username']:
user['publicKey'] = public_key_value
with open(auth.CONFIG_FILE_LOCATION, 'w+') as outfile:
json.dump(data, outfile)
break
outfile.close()
return True
else:
return False
def generatePrivateKeyForServer(auth: Authetication,passphrase: str) -> str:
with open(auth.CONFIG_FILE_LOCATION) as json_file:
data = json.load(json_file)
json_file.close()
private_key = RSA.generate(8192)
public_key = private_key.publickey()
private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=passphrase, pkcs=8, protection="scryptAndAES128-CBC"))
public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8))
data['serverPrivateKey'] = private_key_value
with open(auth.CONFIG_FILE_LOCATION, 'w+') as outfile:
json.dump(data, outfile)
return public_key_value
def generateSourceAddress(index: str) -> chr:
return chr(ord(index[0]) + 17)
if __name__ == '__main__':
auth = Authetication()
auth.initConfig()
serverPublicKey = generatePrivateKeyForServer(auth, 'admin')
auth.saveUser('alma', 'alma')
homeDir = auth.login('alma', 'alma')
generatePrivateKeyForUser(auth, 'alma', 'amla', serverPublicKey, generateSourceAddress(homeDir))
auth.saveUser('citrom', 'citrom')
homeDir = auth.login('citrom', 'citrom')
generatePrivateKeyForUser(auth, 'citrom', 'mortic', serverPublicKey, generateSourceAddress(homeDir))