Advertisement
aolivens

Python Hash File

Sep 10th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. #=====================================#
  2. #==============MODULES================#
  3. #=====================================#
  4.  
  5. import hashlib
  6.  
  7. #=====================================#
  8. #==============VARIABLES==============#
  9. #=====================================#
  10.  
  11. chunk_size = 8192
  12. menu = ["(1) MD5    ", "(2) MD4    ", "(3)  SHA-1  ", "(4) SHA-224", "(5) SHA-256", "(6) SHA-384", "(7) SHA-512", "(8) RIPEMD ", "(9) Exit   "]
  13.  
  14. #=====================================#
  15. #==============FUNCTIONS==============#
  16. #=====================================#
  17.  
  18. #creates hash menu
  19. def hash_menu(menu):
  20.     print "+================+"
  21.     print "|    HASH MENU   |"
  22.     print "+================+"
  23.     for word in menu:
  24.         print "|  %s   |" % word
  25.         print "|----------------|"
  26.     print
  27.  
  28. #reads in file to hash | creates new file and saves hash to file | prints hash to screen
  29. def hash_file(filename, hash_type, var):
  30.     with open(filename, 'rb') as f:
  31.         for data in iter(lambda: f.read(chunk_size), b''):
  32.             hash_type.update(data)
  33.     output = hash_type.hexdigest()
  34.     print
  35.    
  36.     saved_file = str(var) + "_hash_" + filename
  37.     print "Saved %s hash to %s" % (var, saved_file)
  38.     print
  39.    
  40.     file = open(saved_file, "w")
  41.     file.write(filename + ": " + var + ": " + output)
  42.     file.close()
  43.     print "Hash Written Successfully!"
  44.     print
  45.     print "%s: %s" % (var, output)
  46.     print
  47.    
  48. #=====================================#
  49. #=============MAIN PROGRAM============#
  50. #=====================================#
  51.  
  52. def main():
  53.     print "This program hashes a file with a hashing function."
  54.     print
  55.     print
  56.    
  57.     #display hash menu
  58.     hash_menu(menu)
  59.    
  60.     #user select hash type
  61.     choice = input("Please select a hash type (1-9): "
  62.     #user defines file to hash
  63.     filename = raw_input("Please enter a file to hash: ")
  64.    
  65.     #MD5
  66.     if choice == 1:
  67.         var = "MD5"
  68.         hash_type = hashlib.md5()
  69.         hash_file(filename, hash_type, var)
  70.         return
  71.     #MD4
  72.     elif choice == 2:
  73.         var = "MD4"
  74.         hash_type = hashlib.new('md4')
  75.         hash_file(filename, hash_type, var)
  76.         return
  77.     #SHA-1
  78.     elif choice == 3:
  79.         var = "SHA-1"
  80.         hash_type = hashlib.sha1()
  81.         hash_file(filename, hash_type, var)
  82.         return
  83.     #SHA-224
  84.     elif choice == 4:
  85.         var = "SHA-224"
  86.         hash_type = hashlib.sha224()
  87.         hash_file(filename, hash_type, var)
  88.         return
  89.     #SHA-256
  90.     elif choice == 5:
  91.         var = "SHA-256"
  92.         hash_type = hashlib.sha256()
  93.         hash_file(filename, hash_type, var)
  94.         return
  95.     #SHA-384
  96.     elif choice == 6:
  97.         var = "SHA-384"
  98.         hash_type = hashlib.sha384()
  99.         hash_file(filename, hash_type, var)
  100.         return
  101.     #SHA-512
  102.     elif choice == 7:
  103.         var = "SHA-512"
  104.         hash_type = hashlib.sha512()
  105.         hash_file(filename, hash_type, var)
  106.         return
  107.     #RIPEMD
  108.     elif choice == 8:
  109.         var = "RIPEMD"
  110.         hash_type = hashlib.new('ripemd160')
  111.         hash_file(filename, hash_type, var)
  112.         return
  113.     #Exit
  114.     elif choice == 9:
  115.         print "Goodbye. Ending Program..."
  116.         return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement