Last active
February 28, 2024 22:23
-
-
Save cryptocode/7bf963cd20ced8c0bea261f7e50a9f6f 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
import tarfile | |
import hashlib | |
import os | |
import sys | |
import requests | |
import io | |
def compute_hash(tar_path, is_url=False): | |
if is_url: | |
response = requests.get(tar_path) | |
tar = tarfile.open(fileobj=io.BytesIO(response.content), mode='r:gz') | |
else: | |
tar = tarfile.open(tar_path, 'r:gz') | |
with tar: | |
files = sorted([i for i in tar.getnames() if i != './'], reverse=False) | |
hashes = [] | |
for file in files: | |
file_stripped = file[2:] | |
try: | |
f = tar.extractfile(file) | |
if f is not None: | |
content = f.read() | |
executable = b'\x01' if os.access(file, os.X_OK) else b'\x00' | |
hashes.append(hashlib.sha256(file_stripped.encode() + b'\x00' + executable + content).digest()) | |
except Exception as e: | |
print(f"Error processing file {file}: {str(e)}") | |
return hashlib.sha256(b''.join(hashes)).hexdigest() | |
if len(sys.argv) != 2: | |
print("Usage: python3 zig_tar_gz_hash.py <path or url to tar.gz file>") | |
else: | |
path = sys.argv[1] | |
is_url = path.startswith('http://') or path.startswith('https://') | |
prefix = "1220"; | |
print(prefix + compute_hash(path, is_url)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool, works 👍 thanks for checking again