#!/usr/bin/python
# Program to change filename for hexaname in analysis files benigns 
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import hashlib
import os

# Paths in our program
address = '<do not forget>'			# initial Path
results = '<do not forget>'			# The files with name changed were sent to "benign_first_phase/" diretory

# SHA256 Hash Funtion for change the files name:
def sha256_checksum(filename, block_size=65536):
	sha256 = hashlib.sha256()
	with open(filename,'rb') as f:
		for block in iter(lambda: f.read(block_size), b''): 
			sha256.update(block)
	return sha256.hexdigest()

# The program begin here
if __name__ == "__main__":
	print 'Change Hexaname in files starts now.'
	print 'First path'
	files = os.listdir(results)
	n_files = len([name for name in os.listdir(results) if os.path.isfile(os.path.join(results, name))])
	i = 0 	# counter i = 0
	for file in files:
		i += 1
		print '\n',i,'/',n_files
		checksum = sha256_checksum(results+file)	# we call the function "sha256_checkshum", passing the parameters results+file
		print 'The file ',file,' change its name to ',checksum, '.'
		os.rename(results+file,results+checksum)
	# Starts the 2do phase
	print '\nStart the 2nd phase to change name and copy to results file'
	for j in range(2,18):
		print 'Reviewing the directory Analisado',j,'.'
		source = address+str(j)+'/benign_first_phase/'
		files =  os.listdir(source)
		n_files = len([name for name in os.listdir(source) if os.path.isfile(os.path.join(source, name))])
		k = 0	# counter k = 0
		for file in files:
			k += 1
			print '\n',k,'/',n_files
			checksum = sha256_checksum(source+file)	# we call the function "sha256_checkshum", passing the parameters source+file
			print 'The file ',file,' was copy to',source,'/',checksum, '.'
			os.system('cp '+source+file+' '+results+checksum)

print '\ndone!'
print 'end'