Created
May 13, 2022 04:18
-
-
Save mentix02/d50860dbf6a3a0b3936ebfd341cbf362 to your computer and use it in GitHub Desktop.
I shouldn't have to install coreutils on my Mac to compute the sha sums of files, fuck you
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
#!/usr/bin/env python3 | |
import pathlib | |
import hashlib | |
import argparse | |
def compute(path: pathlib.Path, h_name: str = 'sha256') -> str: | |
hasher = hashlib.new(h_name) | |
with open(path, 'rb') as f: | |
for byte_block in iter(lambda: f.read(4096), b''): | |
hasher.update(byte_block) | |
return hasher.hexdigest() | |
def main(): | |
parser = argparse.ArgumentParser(description="compute hashes for files") | |
parser.add_argument("--hash", default="sha256") | |
parser.add_argument("-v", "--version", action="version", version="%(prog)s 0.1") | |
parser.add_argument( | |
"files", type=pathlib.Path, nargs="+", help="compute hashes for these files" | |
) | |
args = parser.parse_args() | |
for file in args.files: | |
print(file, compute(file, args.hash)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment