Skip to content

Instantly share code, notes, and snippets.

@cryptocode
Last active February 28, 2024 22:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cryptocode/7bf963cd20ced8c0bea261f7e50a9f6f to your computer and use it in GitHub Desktop.
Save cryptocode/7bf963cd20ced8c0bea261f7e50a9f6f to your computer and use it in GitHub Desktop.
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))
@guidoschmidt
Copy link

Cool, works 👍 thanks for checking again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment