2022-12-09 12:03:45 +01:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import math
|
|
|
|
import numpy
|
|
|
|
import re
|
2022-12-09 14:52:57 +01:00
|
|
|
import json
|
2022-12-09 13:05:56 +01:00
|
|
|
from .AbstractSplittedSecret import AbstractSplittedSecret
|
2022-12-09 12:03:45 +01:00
|
|
|
|
2022-12-09 20:13:48 +01:00
|
|
|
class Encryption(AbstractSplittedSecret):
|
2022-12-09 12:03:45 +01:00
|
|
|
|
2022-12-09 14:52:57 +01:00
|
|
|
def __init__(self, amount_of_secret_holders, decryption_quota,master_password):
|
2022-12-09 20:13:48 +01:00
|
|
|
super(Encryption, self).__init__()
|
2022-12-09 12:03:45 +01:00
|
|
|
self.amount_of_secret_holders = amount_of_secret_holders
|
|
|
|
self.decryption_quota = decryption_quota
|
2022-12-09 14:52:57 +01:00
|
|
|
self.master_password = master_password
|
2022-12-09 12:03:45 +01:00
|
|
|
self.quota_factor=self.decryption_quota/100
|
|
|
|
self.group_members_amount=math.ceil(self.amount_of_secret_holders * self.quota_factor)
|
2022-12-09 22:37:29 +01:00
|
|
|
self.initializeUserData()
|
|
|
|
self.initializeGroupData()
|
|
|
|
|
|
|
|
def initializeUserData(self):
|
|
|
|
self.user_mapped_data = {}
|
|
|
|
user_count = 1
|
|
|
|
while user_count <= self.amount_of_secret_holders:
|
2022-12-10 12:21:43 +01:00
|
|
|
self.user_mapped_data[str(user_count)] = {"groups":{},"user_password":self.createPassword(self.USER_PASSWORD_LENGTHS),"about":{}}
|
2022-12-09 22:37:29 +01:00
|
|
|
user_count += 1;
|
|
|
|
|
|
|
|
def initializeGroupData(self):
|
|
|
|
self.group_mapped_data = {}
|
|
|
|
|
|
|
|
def addInformationToUser(self,user_id,label,content):
|
|
|
|
self.user_mapped_data[user_id]['about'][label] = content;
|
2022-12-09 12:11:46 +01:00
|
|
|
|
2022-12-09 12:03:45 +01:00
|
|
|
def getStartnumber(self):
|
|
|
|
index = 0
|
|
|
|
start_number = ''
|
|
|
|
while index < self.group_members_amount:
|
|
|
|
start_number += '1'
|
|
|
|
index += 1
|
|
|
|
return int(start_number)
|
|
|
|
|
|
|
|
def getEndnumber(self):
|
|
|
|
index = 0
|
|
|
|
start_number = ''
|
|
|
|
while index < self.group_members_amount:
|
|
|
|
start_number += str(self.amount_of_secret_holders)
|
|
|
|
index += 1
|
|
|
|
return int(start_number)
|
|
|
|
|
2022-12-09 17:32:02 +01:00
|
|
|
def createPassword(self,length):
|
2022-12-09 12:03:45 +01:00
|
|
|
characters = string.ascii_letters + string.digits
|
2022-12-09 17:32:02 +01:00
|
|
|
return (''.join(random.choice(characters) for i in range(length)).upper())
|
2022-12-09 12:03:45 +01:00
|
|
|
|
2022-12-09 12:11:46 +01:00
|
|
|
def isGroupValid(self,password_group_index_str):
|
2022-12-09 13:05:56 +01:00
|
|
|
secret_stakeholders_range=range(1,(self.amount_of_secret_holders+1))
|
|
|
|
valid_numbers = re.compile("([" + ','.join([str(x) for x in secret_stakeholders_range]) + "]{" + str(self.group_members_amount) + "})")
|
2022-12-09 12:03:45 +01:00
|
|
|
unvalid_sequenz = re.compile("(.)\\1+")
|
2022-12-09 12:11:46 +01:00
|
|
|
return re.search(valid_numbers, password_group_index_str) and not re.search(unvalid_sequenz, password_group_index_str)
|
|
|
|
|
2022-12-09 22:37:29 +01:00
|
|
|
def compileContacts(self):
|
|
|
|
contacts = {}
|
|
|
|
for user_id in self.user_mapped_data:
|
|
|
|
contacts[user_id] = self.user_mapped_data[user_id]['about']
|
|
|
|
for user_id in self.user_mapped_data:
|
|
|
|
self.user_mapped_data[user_id]['contacts'] = {}
|
|
|
|
for contact_id in contacts:
|
|
|
|
if contact_id != user_id:
|
|
|
|
self.user_mapped_data[user_id]['contacts'][contact_id] = contacts[contact_id]
|
2022-12-09 14:05:54 +01:00
|
|
|
|
2022-12-09 22:37:29 +01:00
|
|
|
def compileData(self):
|
|
|
|
self.compileContacts()
|
2022-12-09 12:03:45 +01:00
|
|
|
index = self.getStartnumber()
|
|
|
|
while index < self.getEndnumber():
|
|
|
|
password_group_index_str = ''.join(sorted(str(index)))
|
2022-12-09 12:11:46 +01:00
|
|
|
if self.isGroupValid(password_group_index_str):
|
2022-12-09 12:03:45 +01:00
|
|
|
password_group_index_int = int(password_group_index_str)
|
2022-12-09 14:05:54 +01:00
|
|
|
if not password_group_index_int in self.group_mapped_data:
|
|
|
|
self.group_mapped_data[password_group_index_int] = {}
|
|
|
|
self.group_mapped_data[password_group_index_int]['members'] = {}
|
|
|
|
self.group_mapped_data[password_group_index_int]['password'] = ''
|
2022-12-09 12:03:45 +01:00
|
|
|
password = ''
|
|
|
|
for secret_holder_index in password_group_index_str:
|
2022-12-09 14:05:54 +01:00
|
|
|
self.group_mapped_data[password_group_index_int]['members'][secret_holder_index]={}
|
2022-12-10 12:21:43 +01:00
|
|
|
particial_password_length= int(self.OVERALL_PASSWORD_LENGTHS*self.quota_factor);
|
2022-12-09 17:32:02 +01:00
|
|
|
password_part = self.createPassword(particial_password_length)
|
2022-12-09 14:05:54 +01:00
|
|
|
self.group_mapped_data[password_group_index_int]['members'][secret_holder_index] = password_part
|
2022-12-09 12:03:45 +01:00
|
|
|
password += password_part
|
2022-12-09 17:32:02 +01:00
|
|
|
self.user_mapped_data[secret_holder_index]['groups'][password_group_index_str] = password_part
|
2022-12-09 14:05:54 +01:00
|
|
|
self.group_mapped_data[password_group_index_int]['password'] += password
|
|
|
|
index += 1
|
|
|
|
|
2022-12-09 17:32:02 +01:00
|
|
|
def encryptStringToFile(self,text,output_file,password):
|
2022-12-09 20:10:12 +01:00
|
|
|
self.executeCommand('echo \'' + text + '\' | gpg --symmetric --armor --batch --passphrase "' + password + '" -o "' + output_file + '"')
|
2022-12-09 17:32:02 +01:00
|
|
|
|
2022-12-09 21:49:06 +01:00
|
|
|
def encryptGroupFiles(self):
|
2022-12-09 14:05:54 +01:00
|
|
|
for password_group_index_int in self.group_mapped_data:
|
2022-12-10 13:22:09 +01:00
|
|
|
encrypted_group_password_file_path = self.getGroupFilePath(password_group_index_int,AbstractSplittedSecret.TYPE_ENCRYPTED)
|
2022-12-09 20:10:12 +01:00
|
|
|
self.encryptStringToFile(self.master_password,encrypted_group_password_file_path,self.group_mapped_data[password_group_index_int]['password'])
|
2022-12-09 14:52:57 +01:00
|
|
|
|
2022-12-09 17:32:02 +01:00
|
|
|
def encryptToJsonFile(self,data,file_path,password):
|
|
|
|
self.encryptStringToFile(json.dumps(data,ensure_ascii=False), file_path, password)
|
|
|
|
|
2022-12-09 21:49:06 +01:00
|
|
|
def encryptUserData(self):
|
2022-12-09 14:52:57 +01:00
|
|
|
for user_id in self.user_mapped_data:
|
2022-12-10 13:22:09 +01:00
|
|
|
file_path=self.getUserFilePath(user_id,AbstractSplittedSecret.TYPE_ENCRYPTED)
|
2022-12-09 22:37:29 +01:00
|
|
|
data=self.user_mapped_data[user_id]
|
2022-12-09 20:10:12 +01:00
|
|
|
password=self.user_mapped_data[user_id]['user_password']
|
|
|
|
self.encryptToJsonFile(data,file_path,password)
|
2022-12-09 14:05:54 +01:00
|
|
|
|
2022-12-09 21:49:06 +01:00
|
|
|
def encryptAccumulatedData(self):
|
2022-12-10 13:22:09 +01:00
|
|
|
file_path=self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_ENCRYPTED)
|
2022-12-09 17:32:02 +01:00
|
|
|
data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data}
|
|
|
|
self.encryptToJsonFile(data,file_path,self.master_password)
|
2022-12-09 14:52:57 +01:00
|
|
|
|
2022-12-09 21:49:06 +01:00
|
|
|
def encrypt(self):
|
|
|
|
self.encryptUserData()
|
|
|
|
self.encryptAccumulatedData()
|
|
|
|
self.encryptGroupFiles()
|