Python's Hashlib module

Source: Internet
Author: User

The Hashlib module is a python built-in digest algorithm.

There are two ways to call Hashlib:

The first is the Hashlib.new method.

New (name, Data=b ", **kwargs)-Returns a new hash object implementing the given hash function; Initializing the hash using the given binary data.

Import= hashlib.new ("MD5", b'alex3714') Md5obj.hexdigest ()

The second is to create the allowed algorithm directly

Named constructor functions is also available, these is faster than using new (name):

MD5 (), SHA1 (), sha224 (), sha256 (), sha384 (), sha512 (), blake2b (), Blake2s (), sha3_224, sha3_256, sha3_384, sha3_512, shake _128, and shake_256.

More algorithms is available on your platform but the above is guaranteed to exist. See the Algorithms_guaranteed and Algorithms_available attributes to find out what algorithm names can is passed to new ().

ImportHashlib#A string is summarized and a fixed value is obtained, and a multi-medium encryption algorithm is included .Md5obj = HASHLIB.MD5 ()#SummaryMd5obj.update (b'Alex')#The bytes of the string to encryptMd5obj.update (b"3714")#The bytes of the string to encrypt#multiple update is stitched together, then Hexdigest (), and the result is the same as the time of the update#md5obj.update (b ' alex3714)Md5obj.hexdigest ()#turns into 16, the encryption result is 32 bits

User name and password encryption:

noun: collision, verify the consistency of the results of the input and stored cryptographic algorithms based on the results of the input and digest algorithms.

Add salt to prevent collisions by adding random strings.

  Encryption of user names and passwords is usually done by adding salt:

1. Generate random strings based on user name

2. The random string and password together, the summary algorithm, to obtain the final password encryption results

ImportHashlib, Randoml1= List (range (48, 58)) L2= List (range (65, 91)) L3= List (range (97, 123) ) L1.extend (L2) L1.extend (L3) Username="Alex"passwd="alex3714"#generate random strings based on user nameLis ="" forIinchRange (16): v= Random.randint (0, Len (L1)-1) Lis+=chr (L1[v]) passwd+=LISMD5= Hashlib.new ("MD5", Bytes (passwd, encoding="UTF8")) passwd=md5.hexdigest () User=Dict (username=username, user_string=Lis, passwd=passwd,)Print(user)#{' username ': ' Alex ', ' user_string ': ' mnkcvih3avddyp3u ', ' passwd ': ' 35a5a0e38ea22720a78d594d8f02a021 '}

Python's Hashlib module

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.