Skip to content
Snippets Groups Projects
Commit c43dab07 authored by Denis Gremaud's avatar Denis Gremaud
Browse files

OK

parent 17efafea
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
Usage: Usage:
====== ======
python blockchain_gremaud_tefridj.py file_name python blockchain_gremaud_tefridj.py file_name nonce
file_name: le nom du fichier pour commencer la blockchain file_name: le nom du fichier pour commencer la blockchain
nonce: la taille du nonce voulu pour le minage
""" """
__authors__ = ("Denis Gremaud", "Yanis Tefridj") __authors__ = ("Denis Gremaud", "Yanis Tefridj")
...@@ -17,11 +18,10 @@ __version__= "1.0.1" ...@@ -17,11 +18,10 @@ __version__= "1.0.1"
import sys import sys
import hashlib import hashlib
import sys import sys
import math
import os import os
import time import time
NONCE_SIZE = 13 # number of numbre that should be 0 in nonce NONCE_SIZE = 13
"""permet de lire un fichier """permet de lire un fichier
...@@ -58,7 +58,6 @@ def write_file_blockchain(input_file_path, data): ...@@ -58,7 +58,6 @@ def write_file_blockchain(input_file_path, data):
file.write(data) file.write(data)
file.close() file.close()
"""permet d'obtenir le hash d'un fichier en sha256 """permet d'obtenir le hash d'un fichier en sha256
Args: Args:
...@@ -75,18 +74,15 @@ def hash_data(data, print_hash=False): ...@@ -75,18 +74,15 @@ def hash_data(data, print_hash=False):
print('hash : {}'.format(hash_file)) print('hash : {}'.format(hash_file))
return hash_file return hash_file
"""permet de lire un fichier et ensuite de le hash en sha256 def print_last_block(input_file_path, block_current, time):
print("-"*90)
Args: print("Insertion de {}".format(input_file_path))
file_name: le path ou nom du fichier a lire et a hash print("Temps de minage : {} secondes\n".format(time))
print_hash: variable initialiser a false mais si true permet d'afficher le hash print("Numéro du block : {}".format(block_current.block_number[0]))
print("Hash du block précedent : 0x{}".format(block_current.block_prev_hash.hex()))
Returns: print("Nonce : {}".format(int.from_bytes(block_current.nonce, byteorder ='big')))
le hash du contenu du fichier print("Hash du block courant : 0x{}".format(block_current.block_hash))
print("-"*90)
"""
def hash_file(input_file_path, print_hash=False):
return hash_data(read_file_blockchain(input_file_path), print_hash)
class blockchain: class blockchain:
...@@ -118,12 +114,6 @@ class blockchain: ...@@ -118,12 +114,6 @@ class blockchain:
self.first_block = block self.first_block = block
self.total_block += 1 self.total_block += 1
def read(self, input_file_path):
read_b = block(input_file_path)
read_b.block_number, read_b.block_prev_hash = read_file_blockchain(input_file_path, mode=2)
read_b.block_hash = hash_file(input_file_path)
return read_b
class block: class block:
def __init__(self, file_path, block_number=0, block_prev_hash=0x00, block_obj=None, nonce=0): def __init__(self, file_path, block_number=0, block_prev_hash=0x00, block_obj=None, nonce=0):
...@@ -134,41 +124,36 @@ class block: ...@@ -134,41 +124,36 @@ class block:
self.block_obj = block_obj self.block_obj = block_obj
self.block_hash = "" self.block_hash = ""
def print_block(input_file_path, block_current):
print("Insertion de [", input_file_path, "]\n")
print("Numéro du block : ", block_current.block_number[0])
print("Hash du block pointé : 0x", block_current.block_prev_hash.hex())
print("Nonce : ", int.from_bytes(block_current.nonce, byteorder ='big'))
print("Hash du block inséré : 0x", block_current.block_hash)
print("-"*90)
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: # on test si on a spécifié un nom de fichier à trier if len(sys.argv) > 1: # on test si on a spécifié un nom de fichier à trier
input_file_path = sys.argv[1] input_file_path = sys.argv[1]
if len(sys.argv) == 2:
nonce_size = NONCE_SIZE
else:
nonce_size = int(sys.argv[2])
if os.path.exists(input_file_path) == True: if os.path.exists(input_file_path) == True:
if os.path.isdir('blockchain') is not True: if os.path.isdir('blockchain') is not True:
os.mkdir('blockchain') os.mkdir('blockchain')
t = time.time() time_exec = time.time()
blockchain = blockchain(input_file_path, NONCE_SIZE) blockchain = blockchain(input_file_path, nonce_size)
t = time.time() - t time_exec = time.time() - time_exec
print("\nTemps d'insertion : ", t)
input_file_path = os.path.join("blockchain", input_file_path) print_last_block(input_file_path, blockchain.first_block, time_exec)
first_block_read = blockchain.read(input_file_path)
print_block(input_file_path, blockchain.first_block)
input_file_path = input("\nEntrer un nouveau chemin de fichier à insérer : ") input_file_path = input("\nEntrer un nouveau chemin de fichier à insérer : ")
while input_file_path != "exit": while input_file_path != "exit":
if os.path.exists(input_file_path) == True: if os.path.exists(input_file_path) == True:
block_input = block(input_file_path, blockchain.total_block, int(blockchain.first_block.block_hash, 16), blockchain.first_block) block_input = block(input_file_path, blockchain.total_block, int(blockchain.first_block.block_hash, 16), blockchain.first_block)
t = time.time()
time_exec = time.time()
blockchain.insert(block_input) blockchain.insert(block_input)
t = time.time() - t time_exec = time.time() - time_exec
print("\nTemps d'insertion : ", t)
input_file_path = os.path.join("blockchain", input_file_path) print_last_block(input_file_path, blockchain.first_block, time_exec)
block_read = blockchain.read(input_file_path)
print_block(input_file_path, blockchain.first_block)
input_file_path = input("\nEntrer un nouveau chemin de fichier à insérer : ") input_file_path = input("\nEntrer un nouveau chemin de fichier à insérer : ")
else: else:
print("Le fichier spécifié n'existe pas !") # message affiché si le fichier n'existe pas print("Le fichier spécifié n'existe pas !") # message affiché si le fichier n'existe pas
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment