Python hashlib module

Source: Internet
Author: User
Tags hmac sha1

The Hashlib module is used to encrypt related operations, instead of the MD5 module and the SHA module, mainly providing SHA1,SHA224,SHA256,SHA384,SHA512,MD5 algorithms.

MD5 and SHA modules have been discarded in the Python3

What is a digest algorithm? Abstract the algorithm is also called hash algorithm and hashing algorithm. It uses a function to convert any length of data into a fixed length data string (usually represented by a 16-binary string).

Abstract algorithm is a summary function f() for any length of data to data calculate a fixed length of the abstract digest , the purpose is to find out whether the original data has been tampered with.

Abstract the algorithm can point out whether the data has been tampered with, because the digest function is a one-way function, the calculation f(data) is easy, but it is digest very difficult to push back data . Also, making a bit change to the original data will result in a completely different summary of the calculations.

Import Hashlib

MD5 is the most common digest algorithm and is fast enough to generate a fixed byte of bytes, typically represented by a 32-bit 16 binary string.

MD5 = HASHLIB.MD5 () md5.update (' 123456 '. Encode (' Utf-8 ')) print (Md5.hexdigest ()) Calculated summary e10adc3949ba59abbe56e057f20f883e

The result of the SHA1 is a bit byte, which is usually represented by a 40-bit 16 binary string.

SHA1 = HASHLIB.SHA1 () sha1.update (' 123456 '. Encode (' Utf-8 ')) print (Sha1.hexdigest ()) Calculated summary 7c4a8d09ca3762af61e59520943dc26494f8941b

Algorithms that are more secure than SHA1 are SHA256, SHA384, and SHA512, but the more secure the algorithm, the slower it is, and the longer the digest length.

sha256 = hashlib.sha256 () sha256.update (' 123456 '. Encode (' Utf-8 ')) print (Sha256.hexdigest ()) Calculated summary 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

sha384 = hashlib.sha384 () sha384.update (' 123456 '. Encode (' Utf-8 ')) print (Sha384.hexdigest ()) Calculated summary 0a989ebc4a77b56a6e2bb7b19d995d185ce44090c13e2984b7ecc6d446d4b61ea9991b76a4c2f04b1b4d244841449454

sha512 = hashlib.sha512 () sha512.update (' 123456 '. Encode (' Utf-8 ')) print (Sha512.hexdigest ()) Calculated summary ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145 464e2a0bab413

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

MD5 = HASHLIB.MD5 (' Jiami '. Encode (' Utf-8 ')) md5.update (' 123456 '. Encode (' Utf-8 ')) print (Md5.hexdigest ()) Calculated summary 7cea57894be0879c73d39536cd13ead2

Python also has an HMAC module that internally creates keys and content for us to process and then encrypt

Import Hmach = hmac.new (' Jiami '. Encode (' Utf-8 ')) h.update (' 123456 '. Encode (' Utf-8 ')) print (H.hexdigest ()) Calculated summary C9132de0e588824bd284fc34cd6c9dc0

Python hashlib module

Related Article

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.