def generate(): """ CLI to generate a hash from a file and store it :return: """ message, filename_message, q_pressed = file_loader( "Fichier du dossier data à hasher (test.txt par défaut) :", "test.txt", path_data) if q_pressed: return print( "Fichier du dossier data où stocker le hash (test_hash.txt par défaut) :" ) filename_hash = input() if filename_hash == 'q': return if filename_hash == '': filename_hash = 'test_hash.txt' h = SpongeHash() result_hash = h.hash(message) print('Hash de ', filename_message, ' :') print(result_hash) write_file(path.join(path_data, filename_hash), result_hash)
def __save(self): """ Saves the Wallet into a file :return: """ write_file(path.join(path_wallet, self.__user + '_wallet.txt'), self.__user + '\n' + self.__keys_filename)
def __save_blockchain(self): """ Save the current blockchain to a file :return: """ blockchain_text = '' for block in self.__blocks: blockchain_text += str(block) write_file(path.join(path_data, self.__blockchain_file), blockchain_text)
def __init__(self, prime_path: path, n_bits: int = 512, with_generator: bool = True): self.__generator = 0 if path.exists(prime_path): # Load existing prime prime_lines = read_file(prime_path).splitlines() self.__prime = int(prime_lines[0]) if len(prime_lines) > 1: self.__generator = int(prime_lines[1]) else: # Generate a new prime self.__prime = get_prime(n_bits) if with_generator: self.__generator = find_generator(self.__prime) write_file(prime_path, str(self.__prime) + '\n' + str(self.__generator)) else: write_file(prime_path, str(self.__prime))
def __init__(self, keys_filename: str, prime: int = None, generator: int = None): self.__prime = prime self.__generator = generator if path.exists(path.join(path_public, keys_filename)) and path.exists( path.join(path_private, keys_filename)): # Load existing keys public = read_file(path.join(path_public, keys_filename)).splitlines() if public[0] != 'ElGamal': raise Exception('Les clés stockées dans ' + keys_filename + ' ne sont pas des clés ElGamal') self.__public_key = int(public[1]) self.__prime = int(public[2]) self.__generator = int(public[3]) self.__private_key = int( read_file(path.join(path_private, keys_filename)).splitlines()[1]) else: if prime is None or generator is None: raise Exception( 'Le fichier ', keys_filename, " n'existe pas et le nombrer premier et son générateur " "ne sont pas renseignés. Impossible de générer un " "couple de clés publique / privée!") # Generate new keys xorshift = XORShift() self.__private_key = xorshift.getrandbits(512) self.__public_key = square_and_multiply(generator, self.__private_key, prime) # Write new keys to files write_file(path.join(path_private, keys_filename), 'ElGamal\n' + str(self.__private_key)) write_file( path.join(path_public, keys_filename), 'ElGamal\n' + str(self.__public_key) + '\n' + str(prime) + '\n' + str(generator))
def __init__(self, keys_filename: str, prime_couple: list = None): if path.exists(path.join(path_public, keys_filename)) and path.exists( path.join(path_private, keys_filename)): public = read_file(path.join(path_public, keys_filename)).splitlines() if public[0] != 'RSA': raise Exception('Les clés stockées dans ' + keys_filename + ' ne sont pas des clés RSA') self.__e = int(public[1]) self.__n = int(public[2]) self.__d = int( read_file(path.join(path_private, keys_filename)).splitlines()[1]) else: if prime_couple is None: raise Exception( "Le fichier " + keys_filename + " n'existe pas et les nombres premiers ne sont pas renseignés. Impossible de générer un couple de clés publique / privée!" ) p = prime_couple[0] q = prime_couple[1] self.__n = p * q # Euler's totient function phi_n = (p - 1) * (q - 1) # Find e coprime with phi(n) while True: self.__e = random.randint(2, phi_n) if gcd(self.__e, phi_n) == 1: break # Calculate the inverse of e in phi(n) such as e*d = 1 mod(phi(n)) self.__d = inverse(self.__e, phi_n) # print((self.__e * self.__d) % phi_n) # Write new keys to file write_file(path.join(path_private, keys_filename), 'RSA\n' + str(self.__d)) write_file(path.join(path_public, keys_filename), 'RSA\n' + str(self.__e) + '\n' + str(self.__n))
def __save_pending_transactions(self): """ Saves the pending transactions in a file :return: """ if len(self.__pending_transactions): trans_text = '' for trans in self.__pending_transactions: trans_text += str(trans) + '\n' write_file( path.join(path_data, 'pending_transactions_' + self.__blockchain_file), trans_text) else: # No pending transactions -> delete the file if path.exists( path.join(path_data, 'pending_transactions_' + self.__blockchain_file)): os.remove( path.join(path_data, 'pending_transactions_' + self.__blockchain_file))