<reponame>IgorRidanovic/emdeehash
#! /usr/bin/env python
# -*- coding: utf-8 -*-

'''
Meta Fide Emdeehash creates MD5 hashes for arbitrary number of files supplied
as command line arguments. Multiprocessing is not optimized for the case where
the number of input files exceeds the number of available cores. Hashes are
saved in colocated .md5 files as well as in an ongoing log.
www.metafide.com.

MIT License

Copyright (c) 2020 <NAME>, igor ( at ) hdhead.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE'''

import sys
import hashlib
import datetime
import ntpath
import multiprocessing

version = 'V1.0'

# Chunk size for file reading. Experiment with other sizes.
segSize = 4096

def get_args():
	args = sys.argv
	if len(args) == 1:
		sys.exit('Requires at least one filename as argument')
	else:
		return args[1:]

def make_hash(filename):
	hash_md5 = hashlib.md5()
	with open(filename, 'rb') as f:
		for segment in iter(lambda: f.read(segSize), b''):
			hash_md5.update(segment)
    	return hash_md5.hexdigest()

def save_report(filename, hash):
	# Make a responsive character art table first.
	tstamp = timestamp()
	table  = []
	token1 = 'Filename'
	token2 = '<PASSWORD>'
	token3 = 'Generated by Meta <NAME>deehash %s on %s'%(version, tstamp)
	name   = ntpath.basename(filename)

	stretch1 = len(token1) + 2
	stretch2 = len(name) + 2
	# The 2nd column must be at least as wide as the hash
	spacePad1 = len(token3) - len(token1) - len(name) - 1
	spacePad2 = len(token3) - len(hash) - len(token1) - 1
	spacePad3 = 1

	if stretch2 < len(token3):
		stretch2 = len(token3) - len(token1)
	if len(token3) < len(name):
		spacePad1 = 1
		spacePad2 = len(name) - len(hash) + 1
		spacePad3 = len(name) + len(token1) - len(token3) + 3

	table.append('╔' + '═' * stretch1 + '╤' + '═' * stretch2 + '╗')
	table.append('║' + ' ' + token1 + ' ' + '┆' + ' ' + name + ' ' * spacePad1 + '║')
	table.append('╟' + '─' * stretch1  + '┼' + '─' * stretch2 + '╢')
	table.append('║' + ' ' + token2 + ' ' + '┆' + ' ' + hash + ' ' * spacePad2 + '║')
	table.append('╟' + '─' * stretch1 +  '┴' + '─' * stretch2 + '╢')
	table.append('║' + ' ' + token3 + ' ' + ' ' * spacePad3 + '║')
	table.append('╚' + '═' * stretch1 + '═' * (stretch2 + 1) + '╝')

	# Save one .md5 report per input file.
	reportname = filename[:-3] + 'md5'
	with open(reportname, 'w') as f:
		for row in table:
			f.write(row + '\r\n')
		# f.write(filename + '\n' + hash + '\n')

	# Stdout reporting.
	print hash + ' | ' + name

	# Append the history log of all files ever hashed.
	logname = './hashlog.txt'
	entry = tstamp + '\t' + hash + '\t' + name + '\r\n'
	with open(logname, 'a+') as f:
		f.write(entry)

def timestamp():
	fmt='%Y-%m-%d at %H:%M:%S'
	return datetime.datetime.now().strftime(fmt)

def run(filename):
	print 'Processing', filename
	hash = make_hash(filename)
	save_report(filename, hash)


if __name__ == '__main__':
	jobs = []
	for i in get_args():
		p = multiprocessing.Process(target=run, args=(i,))
		jobs.append(p)
		p.start()

#! /usr/bin/env python
# -*- coding: utf-8 -*-

'''
Meta Fide Emdeehash creates MD5 hashes for arbitrary number of files supplied
as command line arguments. Multiprocessing is not optimized for the case where
the number of input files exceeds the number of available cores. Hashes are
saved in colocated .md5 files as well as in an ongoing log.
www.metafide.com.

MIT License

Copyright (c) 2020 <NAME>, igor ( at ) hdhead.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE'''

import sys
import hashlib
import datetime
import ntpath
import multiprocessing

version = 'V1.0'

# Chunk size for file reading. Experiment with other sizes.
segSize = 4096

def get_args():
	args = sys.argv
	if len(args) == 1:
		sys.exit('Requires at least one filename as argument')
	else:
		return args[1:]

def make_hash(filename):
	hash_md5 = hashlib.md5()
	with open(filename, 'rb') as f:
		for segment in iter(lambda: f.read(segSize), b''):
			hash_md5.update(segment)
    	return hash_md5.hexdigest()

def save_report(filename, hash):
	# Make a responsive character art table first.
	tstamp = timestamp()
	table  = []
	token1 = 'Filename'
	token2 = '<PASSWORD>'
	token3 = 'Generated by Meta <NAME>deehash %s on %s'%(version, tstamp)
	name   = ntpath.basename(filename)

	stretch1 = len(token1) + 2
	stretch2 = len(name) + 2
	# The 2nd column must be at least as wide as the hash
	spacePad1 = len(token3) - len(token1) - len(name) - 1
	spacePad2 = len(token3) - len(hash) - len(token1) - 1
	spacePad3 = 1

	if stretch2 < len(token3):
		stretch2 = len(token3) - len(token1)
	if len(token3) < len(name):
		spacePad1 = 1
		spacePad2 = len(name) - len(hash) + 1
		spacePad3 = len(name) + len(token1) - len(token3) + 3

	table.append('╔' + '═' * stretch1 + '╤' + '═' * stretch2 + '╗')
	table.append('║' + ' ' + token1 + ' ' + '┆' + ' ' + name + ' ' * spacePad1 + '║')
	table.append('╟' + '─' * stretch1  + '┼' + '─' * stretch2 + '╢')
	table.append('║' + ' ' + token2 + ' ' + '┆' + ' ' + hash + ' ' * spacePad2 + '║')
	table.append('╟' + '─' * stretch1 +  '┴' + '─' * stretch2 + '╢')
	table.append('║' + ' ' + token3 + ' ' + ' ' * spacePad3 + '║')
	table.append('╚' + '═' * stretch1 + '═' * (stretch2 + 1) + '╝')

	# Save one .md5 report per input file.
	reportname = filename[:-3] + 'md5'
	with open(reportname, 'w') as f:
		for row in table:
			f.write(row + '\r\n')
		# f.write(filename + '\n' + hash + '\n')

	# Stdout reporting.
	print hash + ' | ' + name

	# Append the history log of all files ever hashed.
	logname = './hashlog.txt'
	entry = tstamp + '\t' + hash + '\t' + name + '\r\n'
	with open(logname, 'a+') as f:
		f.write(entry)

def timestamp():
	fmt='%Y-%m-%d at %H:%M:%S'
	return datetime.datetime.now().strftime(fmt)

def run(filename):
	print 'Processing', filename
	hash = make_hash(filename)
	save_report(filename, hash)


if __name__ == '__main__':
	jobs = []
	for i in get_args():
		p = multiprocessing.Process(target=run, args=(i,))
		jobs.append(p)
		p.start()
#! /usr/bin/env python # -*- coding: utf-8 -*- Meta Fide Emdeehash creates MD5 hashes for arbitrary number of files supplied as command line arguments. Multiprocessing is not optimized for the case where the number of input files exceeds the number of available cores. Hashes are saved in colocated .md5 files as well as in an ongoing log. www.metafide.com.  MIT License  Copyright (c) 2020 <NAME>, igor ( at ) hdhead.com  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE # Chunk size for file reading. Experiment with other sizes. # Make a responsive character art table first. # The 2nd column must be at least as wide as the hash # Save one .md5 report per input file. # f.write(filename + '\n' + hash + '\n') # Stdout reporting. # Append the history log of all files ever hashed.