Exemple #1
0
def test_pyca():
	# transfer string data to bytes block
	bytes_block=TypesUtil.string_to_bytes('samuel');

	hash_value=Crypto_Hash.generate_hash(bytes_block)
	print(Crypto_Hash.verify_hash(hash_value, b'samuelx'))
	pass
Exemple #2
0
    def hash_dataset(dataset, keep_labels):
        '''
        Generate hash value of loaded dataset (tensor-->numpy-->string)

        Args:
            dataset: dataset object

        Returns:
            Binary hash value
        '''
        str_dataset=[]
        # For each model's state_dict to get str_dataset
        logger.info("For each dataset's touple to get str_dataset...\n")
        i=0
        for data_tensor, target_tensor in dataset:
            if(target_tensor in keep_labels):
                # conver to touple [data, target]
                value_np = [data_tensor, target_tensor]
                # conver to string, which is used for hash function
                str_dataset.append( str(value_np) )

        # convert string to byte before hash operation
        bytes_block = TypesUtil.string_to_bytes(str(str_dataset))

        # generate hash value based on byte string
        hash_value = Crypto_Hash.generate_hash(bytes_block)

        return hash_value
Exemple #3
0
    def hash_model(model):
    	'''
    	Generate hash value of model data (tensor-->numpy-->string)

        Args:
            model: tensor model object

        Returns:
            Binary hash value
    	'''
    	str_model=[]
    	# For each model's state_dict to get str_model
    	logger.info("For each model's state_dict to get str_model...\n")
    	for param_tensor in model.state_dict():
    		# conver to numpy array
    		value_np = model.state_dict()[param_tensor].numpy()
    		# conver to string, which is used for hash function
    		str_model.append([param_tensor, str(value_np)])

    	# convert string to byte before hash operation
    	bytes_block = TypesUtil.string_to_bytes(str(str_model))

    	# generate hash value based on byte string
    	hash_value = Crypto_Hash.generate_hash(bytes_block)

    	return hash_value
Exemple #4
0
	def verify_indexToken(str_index, filepath):
		# Define ls_time_exec to save executing time to log
		ls_time_exec=[]

		# mark the start time
		start_time=time.time()

		#1) read index data in contract
		token_data=mytoken.getIndexToken(str_index);
		#print(token_data)

		# calculate computational cost
		exec_time=time.time()-start_time
		ls_time_exec.append(format(exec_time*1000, '.3f'))	
		print("Execution time of getIndexToken is:%2.6f" %(exec_time))

		
		# mark the start time
		start_time=time.time()

		#2) extract data from index file
		indexData=IndexPolicy.ExtractData(filepath)
		str_value=str(indexData)
		# calculate computational cost
		exec_time=time.time()-start_time
		ls_time_exec.append(format(exec_time*1000, '.3f'))	
		print("Execution time of extract Index is:%2.6f" %(exec_time))


		# mark the start time
		start_time=time.time()

		#3) calculate hash value of str_value
		# transfer string data to bytes block
		bytes_block = TypesUtil.string_to_bytes(str_value);
		hash_value = Crypto_Hash.generate_hash(bytes_block)

		# compare 
		ret_indexAuth = (str(hash_value)==token_data[1])

		# calculate computational cost
		exec_time=time.time()-start_time
		ls_time_exec.append(format(exec_time*1000, '.3f'))	
		print("Execution time of verifyIndex is:%2.6f" %(exec_time))

		#transfer list to string
		str_time_exec=" ".join(ls_time_exec)
		#print(str_time_exec)
		FileUtil.AddLine('exec_time_authIndex.log', str_time_exec)

		#return index authentication result
		return ret_indexAuth
Exemple #5
0
def test_IndexAuth():
	addr_list = './addr_list.json'
	#set sample record 
	record_block={}
	record_block['id']='1'
	#record_block['value']='samuelxu'

	#extract data from index file
	filepath = './features/0_2_person1/13.2.52.txt'
	filepath0 = './features/0_2_person1/13.4.53.txt'
	indexData=IndexPolicy.ExtractData(filepath)
	record_block['value']=str(indexData)

	#1) read token data
	token_data=mytoken.getIndexToken(record_block['id'])
	print(token_data)

	node_data=mytoken.getAuthorizedNodes();
	print(node_data)

	json_token = IndexPolicy.get_indexToken(record_block['id'])
	print(json_token)

	#2) get hash value for index
	# transfer string data to bytes block
	bytes_block = TypesUtil.string_to_bytes(record_block['value'])

	hash_value = Crypto_Hash.generate_hash(bytes_block)
	hash_str = str(hash_value)
	'''print(hash_value)
	print(hash_str)
	print(token_data[1])'''

	#3) set index token
	#mytoken.setIndexToken(record_block['id'], hash_str);

	#4) set authrozied nodes
	node_address = IndexToken.getAddress('TKB1_node1', addr_list)
	#mytoken.addAuthorizedNodes(node_address)
	#mytoken.removeAuthorizedNodes(node_address)

	#5) verify hash
	#hash_token = token_data[1]
	#print(Crypto_Hash.verify_hash(hash_value, bytes_block))

	print(IndexPolicy.verify_indexToken(record_block['id'],filepath))