-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasher.py
42 lines (31 loc) · 1.11 KB
/
hasher.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
import sys
import os
import subprocess
import shlex
system_os = sys.argv[1]
option = sys.argv[2]
def hashing():
if system_os == 'windows':
md5 = 'md5sum.exe "{}"'.format(file)
sha256 = 'sha256.exe "{}"'.format(file)
if system_os == 'unix':
sha256 = 'sha256sum "{}"'.format(file)
md5 = 'md5sum "{}"'.format(file)
md5_hash = subprocess.check_output(shlex.split(md5)).decode().split()[0]
sha256_hash = subprocess.check_output(shlex.split(sha256)).decode().split()[0]
return md5_hash, sha256_hash
if option == 'folder':
files = []
folder_path = input('folder path: ')
for root, dirs, target_files in os.walk(folder_path):
for file in target_files:
files.append(os.path.join(root, file))
if option == 'files':
files = input('files (separated by spaces): ').split()
with open('hashed.txt', 'w') as f:
for file in files:
md5_hash, sha256_hash = hashing()
if md5_hash and sha256_hash:
f.write(f'{file}\nMD5: {md5_hash}\nSHA256: {sha256_hash}\n\n')
else:
print("missing an output hash")