From 4ee98c21c517e40a31d5eb51ef4cee9fe8b4bb7e Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Fri, 28 Jan 2022 13:19:50 -0500 Subject: add simple hash verifier and renamer --- scripts/verify_hashes.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 scripts/verify_hashes.py (limited to 'scripts') diff --git a/scripts/verify_hashes.py b/scripts/verify_hashes.py new file mode 100755 index 00000000..f555d778 --- /dev/null +++ b/scripts/verify_hashes.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +'''Simple script to verify SHA256 hashes and rename downloaded versions with their version number''' +import json +import requests +import hashlib +from pathlib import Path + + +class DownloadException(Exception): + pass + +def sha256(path): + with open(filename,"rb") as f: + return hashlib.sha256(f.read()).hexdigest() + +url = 'https://binary.ninja/js/hashes.json' +r = requests.get(url) +results = json.loads(r.text) +if not results['version']: + raise DownloadException('Hash file does not exist or is incomplete.') + +version = results["version"] +for filename in results["hashes"]: + hash = results["hashes"][filename] + binfile = Path(filename) + if binfile.is_file(): + if sha256(binfile) == hash: + newname = f"{filename[0:-4]}-{version}{filename[-4:]}" + print(f"SHA256 matches for {filename}, renaming to {newname}") + binfile.rename(newname) + else: + print(f"HASH FAILED for {filename}") + else: + print(f"No {filename} found") -- cgit v1.3.1