Skip to content

Instantly share code, notes, and snippets.

@petri
Forked from Zireael-N/Checksum.py
Last active January 24, 2023 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save petri/4a442d4f3ff4e4427bc9933daecf6aba to your computer and use it in GitHub Desktop.
Save petri/4a442d4f3ff4e4427bc9933daecf6aba to your computer and use it in GitHub Desktop.
Python script that calculates SHA1, SHA256, MD5 checksums of a given file. Similar to what you might accomplish with eg. ”openssl dgst -sha512 -binary my-downloaded-file.zip | base64”
#!/usr/bin/python
import hashlib
import os
import sys
if len(sys.argv) < 2:
sys.exit('Usage: %s filename' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: File "%s" was not found!' % sys.argv[1])
with open(sys.argv[1], 'rb') as f:
contents = f.read()
print("SHA1: %s" % hashlib.sha1(contents).hexdigest())
print("SHA256: %s" % hashlib.sha256(contents).hexdigest())
#md5 accepts only chunks of 128*N bytes
md5 = hashlib.md5()
for i in range(0, len(contents), 8192):
md5.update(contents[i:i+8192])
print("MD5: %s" % md5.hexdigest())
input("Press enter to terminate the program")
@petri
Copy link
Author

petri commented Jul 10, 2018

Note that sometimes the value to verify is given as a base64-encoded digest, rather than 'plain' hexdigest. Modify the code above accordingly.

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