def sign(self, msg): # Calculate the hash msg = str(msg) hashed_message = Integer(calculate_hash(msg)) # Calculate the signature of the hash and return signed_message=power_mod(hashed_message,self.d,self.N) return signed_message
def mine(last_block): index = int(last_block.index) + 1 timestamp = datetime.utcnow() # random string for now data = "I block #%s" % (int(last_block.index) + 1) prev_hash = last_block.hash nonce = 0 block_hash = calculate_hash(index, prev_hash, data, timestamp, nonce) while str(block_hash[0:num_zeores]) != '0' * num_zeores: nonce += 1 block_hash = calculate_hash(index, prev_hash, data, timestamp, nonce) block_data = {} block_data['index'] = int(last_block.index) + 1 block_data['timestamp'] = timestamp block_data['data'] = "I block #%s" % last_block.index block_data['prev_hash'] = last_block.hash block_data['hash'] = block_hash block_data['nonce'] = nonce return Block(**block_data)
def verify(self, msg): # Split the message to get the signature and the message signed_hash, msg = msg.split('@') signed_hash = Integer(signed_hash) # Recalculate the hash of the message recalculated_hash = Integer(calculate_hash(msg)) # Recompute the hash from the signed hash retrieved_hash = power_mod(signed_hash, self.e, self.N) # Compare the recomputed hash with the recalculated hash if retrieved_hash != recalculated_hash: return False return msg