Md5 encryption method for python files,
This document describes the md5 encryption method for python files. We will share this with you for your reference. The details are as follows:
Simple mode:
From hashlib import md5def md5_file (name): m = md5 () a_file = open (name, 'rb') # Read File Content in binary format m. update (a_file.read () a_file.close () return m. hexdigest () if _ main _ = '_ init _': print md5_file ('d:/test.txt ')
Faster speed of large files
#!/usr/bin/python#encoding=utf-8import ioimport sysimport hashlibimport stringdef printUsage(): print ('''''Usage: [python] pymd5sum.py <filename>''')def main(): if(sys.argv.__len__()==2): #print(sys.argv[1]) m = hashlib.md5() file = io.FileIO(sys.argv[1],'r') bytes = file.read(1024) while(bytes != b''): m.update(bytes) bytes = file.read(1024) file.close() #md5value = "" md5value = m.hexdigest() print(md5value+"\t"+sys.argv[1]) #dest = io.FileIO(sys.argv[1]+".CHECKSUM.md5",'w') #dest.write(md5value) #dest.close() else: printUsage()main()
I hope this article will help you with Python programming.
Articles you may be interested in:
- Use md5sum in Python to check the sharing of the same file code in the directory
- Example of using the md5, sha, and crypt encryption modules of Python
- Example of using MD5 to encrypt a string in Python
- Use Python and MD5 to detect website Trojans
- Python MD5 file generation code
- Python implements the RSA encryption (decryption) Algorithm
- Use python to encrypt your own password
- Python Implementation Method for RC4 file encryption
- How to Implement file snapshot encryption and protection in python
- Details about how to implement DES encryption and decryption in python
- Python DES-based encryption and decryption instance
- Example Analysis of base64 encryption and decryption methods in python