Skip to content

Instantly share code, notes, and snippets.

@mentix02
Created May 13, 2022 04:18
Show Gist options
  • Save mentix02/d50860dbf6a3a0b3936ebfd341cbf362 to your computer and use it in GitHub Desktop.
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
#!/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