-
-
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”
This file contains hidden or 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/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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that sometimes the value to verify is given as a base64-encoded digest, rather than 'plain' hexdigest. Modify the code above accordingly.