Created
December 30, 2015 22:14
-
-
Save 0x27/4e93da9d944644629b5c to your computer and use it in GitHub Desktop.
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/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