#coding: GBK
#md5SHA1 file Check value calculation
# How to use: python file . PY Validating Files
Import Hashlib
Import Sys
Import OS
defmd5_sum (filename): # Check Value Method
fd=open (filename, "RB") # Open File
Fd.seek (0) # move the file hit tag to Offset the location
line=fd.readline () # read the first line of the file into Line
#md5 Check value calculation
MD5=HASHLIB.MD5 ()
Md5.update (line)
#SHA1 Check value calculation
SHA1=HASHLIB.SHA1 ()
Sha1.update (line)
While line : # Looping through Files
Line=fd.readline ()
Md5.update (line)
Sha1.update (line)
fmd5=md5.hexdigest () # Generating Files MD5 Checksum value
fsha1=sha1.hexdigest () # Generating Files SHA1 Checksum value
FSUM=[FMD5,FSHA1]
Fd.close ()
Return fsum
defm_help (): # Program Help Methods
St_help= ""
****************************************
* * MD5 SHA1 Verifying use Help
** Input Format python md5.py file
* * Example:python md5.py D:/1/1.txt
‘‘‘
Print St_help
if __name__ = = "__main__":
Try
Filename=sys.argv[1] # Get input value
if filename== '--help ' orfilename== ': # go to the Help menu
M_help ()
elif os.path.isfile (filename): # go to File check menu
Fmd5=md5_sum (filename)
print ' File: {} '. Format (filename)
print ' MD5: {} '. Format (fmd5[0])
print ' SHA1: {} '. Format (fmd5[1])
Else: # go to the error menu
Er= ""
========== Input Error ===================== ""
Print ER
M_help ()
Except Exception:
M_help ()
This article is from the "Tuk-line" blog, please make sure to keep this source http://lovexm.blog.51cto.com/3567383/1718052
Python file MD5 SHA1 checksum calculation