summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAetias <aetias@outlook.com>2025-01-18 15:50:47 +0100
committerAetias <aetias@outlook.com>2025-01-18 15:50:47 +0100
commitc81c165c37b64fab29b33a0a5c76f06efe53aa73 (patch)
tree66a5522f1036214fab8bca27f459f81a2fb844b1 /tools
parentfe4c283ba249c5c676b75542c83dfbb829c768b3 (diff)
Verify SHA1 checksum
Diffstat (limited to 'tools')
-rw-r--r--tools/configure.py16
-rw-r--r--tools/sha1.py29
2 files changed, 45 insertions, 0 deletions
diff --git a/tools/configure.py b/tools/configure.py
index 77e3ba53..2a8d77ee 100644
--- a/tools/configure.py
+++ b/tools/configure.py
@@ -179,6 +179,12 @@ def main():
)
n.newline()
+ n.rule(
+ name="sha1",
+ command=f"{PYTHON} tools/sha1.py $in -c $sha1_file"
+ )
+ n.newline()
+
game_build = build_path / game_version
game_extract = extract_path / game_version
@@ -249,6 +255,16 @@ def add_mwld_and_rom_builds(n: ninja_syntax.Writer, game_build: Path, game_confi
)
n.newline()
+ n.build(
+ inputs=rom_file,
+ rule="sha1",
+ variables={
+ "sha1_file": str(Path(rom_file).with_suffix(".sha1"))
+ },
+ outputs="sha1",
+ )
+ n.newline()
+
def add_mwcc_builds(n: ninja_syntax.Writer, game_version: str, game_build: Path, mwcc_implicit: list[Path]):
for source_file in get_c_cpp_files([src_path, libs_path]):
diff --git a/tools/sha1.py b/tools/sha1.py
new file mode 100644
index 00000000..8cd6edd8
--- /dev/null
+++ b/tools/sha1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+from pathlib import Path
+import argparse
+import hashlib
+import sys
+
+def eprint(*args, **kwargs):
+ print(*args, file=sys.stderr, **kwargs)
+
+parser = argparse.ArgumentParser(description="Computes and verifies a SHA1 checksum")
+parser.add_argument('file', help="Input file to verify")
+parser.add_argument('-c', type=str, dest='checksum_file', required=False, help='Checksum file')
+args = parser.parse_args()
+
+file_path = Path(args.file)
+checksum_path = Path(args.checksum_file)
+
+with checksum_path.open('r') as file:
+ target_sha1, _ = file.readline().split(' ', 1)
+
+with file_path.open('rb') as file:
+ file_sha1 = hashlib.sha1(file.read()).hexdigest()
+
+if target_sha1 != file_sha1:
+ eprint(f"{file_path}: FAILED")
+ exit(1)
+else:
+ eprint(f"{file_path}: OK")