Last active
December 27, 2019 22:43
-
-
Save xioustic/0702a491990a4cc802565723adfe936b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This tool calculates hashes for ISO files using 7z's hashing capabilities. | |
Optionally it prompts the user to verify that the hash is valid (usually by checking Google). | |
TODO: | |
- Gather info from user on the ISO file | |
- Distribution, Version, Architecture, URL Source(s), Hashes (Computed), Notes (Specifically, How Was Verified?) | |
[s] skip | |
[l] mark legacy | |
[d] set Distro | |
[v] set Version | |
[a] set Arch | |
[n] set Note | |
[c] recalculate hash | |
- if change from known, prompt | |
- if no change, let user know | |
- if not previously known, add | |
[C] mark hashes verified | |
""" | |
import os | |
import re | |
import subprocess | |
CRC32_HASH_MATCH = r"[0-9A-F]{8}" | |
CRC64_HASH_MATCH = r"[0-9A-F]{16}" | |
SHA256_HASH_MATCH = r"[0-9A-F]{64}" | |
SHA1_HASH_MATCH = r"[0-9A-F]{40}" | |
BLAKE2sp_HASH_MATCH = r"[0-9A-F]{64}" | |
PROMPT_FOR_VALID = True | |
SKIP_LEGACY = True | |
_7Z_HASH_MATCH = ( | |
r"^" | |
+ " ".join( | |
[ | |
CRC32_HASH_MATCH, | |
CRC64_HASH_MATCH, | |
SHA256_HASH_MATCH, | |
SHA1_HASH_MATCH, | |
BLAKE2sp_HASH_MATCH, | |
] | |
) | |
+ r"\s+(\d+)\s+(.*)$" | |
) | |
_7Z_RE = re.compile(_7Z_HASH_MATCH) | |
if os.name == "nt": | |
import msvcrt | |
def getch(): | |
return msvcrt.getwch() | |
else: | |
import sys, tty, termios | |
def getch(): | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
for root, dirs, files in os.walk("."): | |
if SKIP_LEGACY and 'Legacy' in root: | |
print('Skipping Legacy directory', root) | |
continue | |
else: print('Working on directory:', root) | |
isos = [x for x in files if x.endswith(".iso")] | |
for iso in isos: | |
iso_path = os.path.join(root, iso) | |
iso_hashes_path = iso_path + ".hashes" | |
print("working on", iso_path) | |
if os.path.exists(iso_hashes_path): | |
print("path exists:", iso_hashes_path) | |
with open(iso_hashes_path, "r") as f: | |
data = f.read() | |
if 'Valid' in data: | |
print('Already hashed & marked for validity, continuing...') | |
print([x for x in data.split('\n') if 'Valid' in x][0]) | |
continue | |
else: | |
data = subprocess.run( | |
["7z", "h", "-scrc*", iso_path], capture_output=True, text=True | |
).stdout | |
with open(iso_hashes_path, 'w') as f: f.write(data) | |
if PROMPT_FOR_VALID: | |
lines = data.split("\n") | |
sha1 = [x for x in lines if x.startswith('SHA1')][0].split(' ')[-1] | |
sha2 = [x for x in lines if x.startswith('SHA256')][0].split(' ')[-1] | |
subprocess.Popen(['clip'], stdin=subprocess.PIPE).communicate(sha2.encode()) | |
print(data) | |
print(iso_path) | |
print(sha2) | |
print('Are these hashes valid? ([y]es/[n]o/[s]kip]/[d]elete/[q]uit/sha[1]/sha[2]') | |
while True: | |
answer = getch() | |
if answer.startswith('y'): | |
lines += ['Valid: true'] | |
print('Marking valid @', iso_hashes_path) | |
break | |
if answer.startswith('n'): | |
lines += ['Valid: false'] | |
print('Marking invalid @', iso_hashes_path) | |
break | |
if answer.startswith('s'): | |
print('Skipping') | |
break | |
if answer.startswith('d'): | |
print('Deleting') | |
os.unlink(iso_path) | |
break | |
if answer.startswith('q'): | |
print('Quitting') | |
sys.exit() | |
if answer.startswith('1'): | |
subprocess.Popen(['clip'], stdin=subprocess.PIPE).communicate(sha1.encode()) | |
print('Copied SHA1 to Clipboard') | |
if answer.startswith('2'): | |
subprocess.Popen(['clip'], stdin=subprocess.PIPE).communicate(sha2.encode()) | |
print('Copied SHA2 to Clipboard') | |
with open(iso_hashes_path, 'w') as f: | |
f.write('\n'.join(lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment