Advertisement
Guest User

hash

a guest
Nov 15th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. # Script: file_hash.py
  2. # Desc:   Generate file hash signature - start code
  3. # modified: 12/11/18 (PEP8 compliance)
  4. #
  5. import sys
  6. import os
  7. import hashlib
  8.  
  9.  
  10. def get_hash(filename):
  11.     """prints a hex hash signature of the file passed in as arg"""
  12.     try:
  13.         # Read File
  14.         with open(filename,'rb') as f:
  15.  
  16.              
  17.  
  18.         # Generate Hash Signature
  19.             file_content=f.read()
  20.             md5_obj=hashlib.md5(file_content)
  21.             file_hashsig=md5_obj.hexdigest()
  22.        
  23.  
  24.             print(f'[+] get_hash() file: {filename} ', end='')
  25.             print(f'hash_sig: {file_hashsig}')
  26.             return file_hashsig
  27.          
  28.  
  29.     except Exception as err:
  30.         print(f'[-] {err}')
  31.  
  32.    
  33. def main():
  34.     # Test case
  35.     sys.argv.append(r'c:\atext.txt')
  36.     # Check args
  37.     if len(sys.argv) != 2:
  38.         print('[-] usage: file_hash filename')
  39.         sys.exit(1)
  40.  
  41.     filename = sys.argv[1]
  42.     get_hash(filename)
  43.  
  44.  
  45. if __name__ == '__main__':
  46.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement