-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashScript.py
117 lines (97 loc) · 4.38 KB
/
hashScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
"""
version 1.0
@author: @manobarssa
"""
#!python3
import argparse, hashlib, os
def stringHasher(txt,algo):
if algo.lower() == 'shake_256':
return getattr(hashlib, algo.lower())(txt.encode()).hexdigest(256)
elif algo.lower() == 'shake_128':
return getattr(hashlib, algo.lower())(txt.encode()).hexdigest(128)
else:
return getattr(hashlib, algo.lower())(txt.encode()).hexdigest()
def fileHasher(path, algo):
block = 52428800
file_hash = getattr(hashlib, algo.lower())()
with open(path, 'rb') as f:
fb = f.read(block)
while len(fb) > 0:
file_hash.update(fb)
fb = f.read(block)
if algo.lower() == 'shake_256':
return file_hash.hexdigest(256)
elif algo.lower() == 'shake_128':
return file_hash.hexdigest(128)
else:
return file_hash.hexdigest()
def main():
algs = ', '.join(hashlib.algorithms_guaranteed) + '''. Two or more separated
by comas (ex sha1,sha256). Or all.'''
parser = argparse.ArgumentParser(description= '''
Hash a file, a list of files, a string or list of strings.
''')
parser.add_argument('-a', dest='alg', metavar='Algorithm', type=str,
help=algs, required=True)
parser.add_argument('-o', dest='outfile', metavar='Out File',
help='File path to write the hashes.')
#add a muttyally exclusive group
mode = parser.add_mutually_exclusive_group()
mode.add_argument('-f', dest='file', metavar='File',
help='Path to file or * to all under current Work Directory.',)
mode.add_argument('-s', dest='string', metavar='String',
help='String to hash.')
mode.add_argument('-fL', dest='fileList', metavar='File List',
help='''Path to text file with files (paths) to hash.
-o flag and a filepath to write results are required.''')
mode.add_argument('-sL', dest='stringList', metavar='String List',
help='''Path to text file with strings (lines) to hash.
-o flag and a filepath to write results are required.''')
args = parser.parse_args()
if args.alg.lower() == 'all':
algList = list(hashlib.algorithms_guaranteed)
algList.sort()
else:
algList = args.alg.lower().split(',')
algList.sort()
#Hash string
if args.string is not None and args.outfile is not None:
string = args.string
with open(args.outfile,'w') as f:
for alg in algList:
f.write('{}:{}:{}'.format(string, alg, stringHasher(string, alg))+'\n')
elif args.string is not None and args.outfile is None:
string = args.string
for alg in algList:
print('{}:{}:{}'.format(string, alg, stringHasher(string, alg)))
#Hash a list of strings from a file
if args.stringList is not None:
with open(args.stringList) as fr:
stringList = fr.readlines()
with open(args.outfile, 'w') as fw:
for string in stringList:
for alg in algList:
fw.write('{}:{}:{}'.format(string.replace('\n',''), alg, stringHasher(string, alg))+'\n')
#Hash files
if args.file == '*':
fileList = [x for x in os.listdir() if os.path.isfile(x)]
elif args.file is not None:
fileList = [args.file]
if args.outfile is not None:
with open(args.outfile, 'w') as f:
for alg in algList:
for file in fileList:
try:
f.write('{}:{}:{}'.format(file, alg, fileHasher(file, alg))+'\n')
except:
pass #to do ----> print error msgs to file
elif args.outfile is None and args.file is not None:
for alg in algList:
for file in fileList:
try:
print('{}:{}:{}'.format(file, alg, fileHasher(file, alg)))
except:
pass #to do ----> print error msgs
if __name__ == '__main__':
main()