Advertisement
abdulfatirs

ChecksumChecker.py | MD5 Checksum of a file in Python

Mar 19th, 2014
650
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. # E-Mail: abdulfatirs@gmail.com
  5.  
  6. from threading import Thread
  7. import hashlib
  8. import optparse
  9.  
  10. def getChecksum(file_path,check_hash):
  11.     file_handle = open(file_path,"rb")
  12.     _md5 = hashlib.md5()
  13.     while True:
  14.         _buffer = file_handle.read()
  15.         if not _buffer:
  16.             break
  17.         _md5.update(_buffer)
  18.     digest = _md5.hexdigest()
  19.     print "[+] File's MD5 checksum is: "+digest
  20.     if (check_hash != None):
  21.         if(check_hash.lower() == digest):
  22.             print "[+] Hash matched: The file is authentic."
  23.         else:
  24.             print "[-] Hash mis-match: The file is not authentic."
  25.  
  26. def Main():
  27.     parser = optparse.OptionParser('usage: %prog -f <filename> [-m <md5 hash>]')
  28.     parser.add_option('-f', dest='file_path', type='string', help='Please specify a file')
  29.     parser.add_option('-m', dest='check_hash', type='string')
  30.     (options,arg) = parser.parse_args()
  31.     if (options.file_path == None):
  32.         print parser.usage
  33.         exit(0)
  34.     else:
  35.         file_path = options.file_path
  36.         check_hash = options.check_hash
  37.     print "[*] Hashing file '"+ file_path +"'...."
  38.     hash_thread = Thread(target=getChecksum,args=(file_path,check_hash))
  39.     hash_thread.start()
  40.     hash_thread.join()
  41. if __name__  == '__main__':
  42.     Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement