abdulfatirs

ChecksumChecker.py | MD5 Checksum of a file in Python

Mar 19th, 2014
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # ChecksumChecker.py
  3. # Author: Abdul Fatir
  4.  
  5. from threading import Thread
  6. import hashlib
  7. import optparse
  8.  
  9. def getChecksum(file_path,check_hash):
  10.     file_handle = open(file_path,"rb")
  11.     _md5 = hashlib.md5()
  12.     while True:
  13.         _buffer = file_handle.read()
  14.         if not _buffer:
  15.             break
  16.         _md5.update(_buffer)
  17.     digest = _md5.hexdigest()
  18.     print "[+] File's MD5 checksum is: "+digest
  19.     if (check_hash != None):
  20.         if(check_hash.lower() == digest):
  21.             print "[+] Hash matched: The file is authentic."
  22.         else:
  23.             print "[-] Hash mis-match: The file is not authentic."
  24.  
  25. def Main():
  26.     parser = optparse.OptionParser('usage: %prog -f <filename> [-m <md5 hash>]')
  27.     parser.add_option('-f', dest='file_path', type='string', help='Please specify a file')
  28.     parser.add_option('-m', dest='check_hash', type='string')
  29.     (options,arg) = parser.parse_args()
  30.     if (options.file_path == None):
  31.         print parser.usage
  32.         exit(0)
  33.     else:
  34.         file_path = options.file_path
  35.         check_hash = options.check_hash
  36.     print "[*] Hashing file '"+ file_path +"'...."
  37.     hash_thread = Thread(target=getChecksum,args=(file_path,check_hash))
  38.     hash_thread.start()
  39.     hash_thread.join()
  40. if __name__  == '__main__':
  41.     Main()
Advertisement
Add Comment
Please, Sign In to add comment