Python's Hashlib module MD5 encryption

Source: Internet
Author: User

Source:

    • Https://www.cnblogs.com/UnGeek/archive/2013/03/30/2990876.html
    • Https://www.cnblogs.com/weiman3389/p/6056305.html
    • Https://www.cnblogs.com/yyds/p/7072492.html
    • Add Salt--https://www.zhihu.com/question/20299384

First, the basic use

1 ImportHashlib2INP = input ('input:>>')3 4 #Create a MD5 cryptographic object5hash =hashlib.md5 ()6 #generate MD5 encrypted string7Hash.update (Bytes (inp,encoding='Utf-8'))8 #print out the MD5 encrypted string9 Print(Hash.hexdigest ())
using Hashlib
    • Hash.update (data)
      • Update the data to be computed by the hash object, multiple calls to the cumulative effect
      • such as M.update (a); M.update (b) equivalent to M.update (A+B)
    • Hash.hexdigest ()
      • Returns summary information for all data passed to the update () function (also known as data thumbprint)
      • string in hexadecimal format
    • Hash.digest ()
      • Returns summary information for all data passed to the update () function
      • string in binary format

Second, Hashlib module use steps

  • 1, get a hash algorithm corresponding to the hash object (for example, the name is hash)
    • Hashlib.new (hash algorithm name, initial access information)
      • Get this hash object, such as Hashlib.new (' MD5 ', ' hello '), hashlib.new (' SHA1 ', ' hello ')
    • Hashlib. hash algorithm name ()
      • Gets the hash object, such as HASHLIB.MD5 (), HASHLIB.SHA1 ()
  • 2. Set/Append input information
    • The update (input information) method that calls the obtained hash object can set or append input information and call the method multiple times, which is equivalent to putting the parameter of each pass as a parameter to the update () method.
    • Multiple calls are cumulative, not overlay, see hash.update ()
  • 3. Get a summary of the input information
    • Call the Digest () method of the obtained hash object or the hexdigest () method to get summary information for the string parameter passed to the update () method.
    • The summary information returned by the digest () method is a binary-formatted string that may contain non-ASCII characters, a null byte, and the length of the string can be obtained by hashing the object's Hash.disgest_size property
    • The summary information returned by the Hexdigest () method is a hexadecimal-formatted string that contains only 16 digits, and is twice times longer than Digest (), which can be used for message security interactions or other non-binary environments .

Third, optimize

  1, anti-collision library, using "Add salt" encryption

1 ImportHashlib2INP = input ('input:>>')3 #Create a MD5 encryption object, and ' Add salt ' encryption, anti-collision library Inverse Solution4hash = HASHLIB.MD5 ('Super'. Encode ('Utf-8'))5 #generate MD5 encrypted string6Hash.update (Bytes (inp,encoding='Utf-8'))7 #print out the MD5 encrypted string8 Print(Hash.hexdigest ())
"Add salt"

 2, a sentence

1 hash = hashlib.md5 (Inp.encode ('utf-8')). Hexdigest ()
a sentence

Python's Hashlib module MD5 encryption

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.