The Hashlib of Python3

Source: Internet
Author: User

Brief introduction:

Used for cryptographic related operations, instead of the MD5 module and the SHA module, mainly provides the SHA1,SHA224,SHA256,SHA384,SHA512,MD5 algorithm.
The MD5 and SHA modules have been discarded in python3, and the use of MD5 and SHA is briefly explained.

What is a digest algorithm?

Abstract algorithm, also known as hashing algorithm, hashing algorithm. It uses a function to convert any length of data into a data string of length headspace (usually represented by a 16-binary string) for encrypting related operations.

Application: 1. MD5 encryption
1 hash = HASHLIB.MD5 () 2 hash.update (' admin '. Encode (' Utf-8 ')) 3 print (Hash.hexdigest ()) 4 21232f297a57a5a743894a0e4a801fc3
2. SHA1 encryption
1 hash = HASHLIB.SHA1 () 2 hash.update (' admin '. Encode (' Utf-8 ')) 3 print (Hash.hexdigest ()) 4 d033e22ae348aeb5660fc2140aec35850c4da997
3. SHA256 Encryption
1 hash = hashlib.sha256 () 2 hash.update (' admin '. Encode (' Utf-8 ')) 3 print (Hash.hexdigest ()) 4 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
4. sha384 Encryption
1 hash = hashlib.sha384 () 2 hash.update (' admin '. Encode (' Utf-8 ')) 3 print (Hash.hexdigest ()) 4 9ca694a90285c034432c9550421b7b9dbd5c0f4b6673f05f6dbce58052ba20e4248041956ee8c9a2ec9f10290cdc0782
5. SHA512 Encryption
1 hash = hashlib.sha512 () 2 hash.update (' admin '. Encode (' Utf-8 ')) 3 print (Hash.hexdigest ()) 4 c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8d E9077ec
6. ' Add salt ' encryption

Although the above encryption algorithm is very powerful, but still has the flaw, through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.

1 ######  MD5 encryption ########### #2 hash = hashlib.md5 (' python '. Encode (' Utf-8 ')) 3 hash.update (' admin '. Encode (' Utf-8 ')) 4 Print (Hash.hexdigest ()) 5 75b431c498b55557591f834af7856b9f
7. HMAC Encryption

The HMAC internally handles the key and content that we created after the encryption

1 import Hmac2 h = hmac.new (' python '. Encode (' Utf-8 ')) 3 h.update (' HelloWorld '. Encode (' Utf-8 ')) 4 print (H.hexdigest ()) 5 B3b867248bb4cace835b59562c39fd55

8. Get the MD5 of the file
123456789101112131415161718 importhashlibdefmd5sum(filename):        """        用于获取文件的md5值        :param filename: 文件名        :return: MD5码        """        ifnotos.path.isfile(filename):  # 如果校验md5的文件不是文件,返回空            return        myhash =hashlib.md5()        f =open(filename, ‘rb‘)        whileTrue:            b =f.read(8096)            ifnotb:                break            myhash.update(b)           f.close()        returnmyhash.hexdigest()

  

The Hashlib of Python3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.