Skip to content

Instantly share code, notes, and snippets.

@0x27
Created December 30, 2015 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x27/4e93da9d944644629b5c to your computer and use it in GitHub Desktop.
Save 0x27/4e93da9d944644629b5c to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
# coding: utf-8
# Hash a file using a few algos.
# Written for a lab.
import hashlib
import sys
def hashfile(file, algo):
bs = 65536
buffer = file.read(bs)
while len(buffer) > 0:
algo.update(buffer)
buffer = file.read(bs)
return algo.hexdigest()
def main(args):
if len(args) !=2:
sys.exit('use: %s file' %(args[0]))
print "{+} Generating hashes for %s" %(args[1])
print "{*} MD5: %s" %(hashfile(file=open(args[1], 'rb'), algo=hashlib.md5()))
print "{*} SHA1: %s" %(hashfile(file=open(args[1], 'rb'), algo=hashlib.sha1()))
print "{*} SHA256: %s" %(hashfile(file=open(args[1], 'rb'), algo=hashlib.sha256()))
# add more hash algos here if you want and if hashlib supports them...
if __name__ == "__main__":
main(args=sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment