Hashlib of common Python modules

Source: Internet
Author: User

Python's Hashlib module provides a number of cryptographic algorithms, which describe the simple use case of hashlib and encrypt the data using Hashlib's MD5 algorithm.

Import Hashlibhash = Hashlib.md5 () #md5对象, MD5 can not be reversed, but the encryption is fixed, that is, the relationship is one by one corresponding, so there is a flaw, can be hit out hash.update (bytes (' admin ', encoding= ' Utf-8 ')) #要对哪个字符串进行加密, put here print (Hash.hexdigest ()) #拿到加密字符串 # hash2=hashlib.sha384 () #不同算法, hashlib many encryption algorithms # Hash2.update (bytes (' admin ', encoding= ' Utf-8 ')) # Print (Hash.hexdigest ()) Hash3 = hashlib.md5 (bytes (' Abd ', encoding= ' Utf-8 ') "If there are no parameters, so md5 obey a rule, generate the same correspondence, if the addition of parameters, is the original encryption on the basis of another layer of encryption, so that the parameters only know, to prevent the pool, because others will never get this parameter" Hash3.update (bytes (' admin ', encoding= ' Utf-8 ')) print (Hash3.hexdigest ())

To execute the results

Write a basic example of password encryption using MD5 for user login website, deepen understanding

#hashlib简单使用def MD5 (ARG): #这是加密函数, the function to be passed in is encrypted    md5_pwd = hashlib.md5 (bytes (' Abd ', encoding= ' Utf-8 '))    md5_ Pwd.update (bytes (arg,encoding= ' utf-8 '))    return md5_pwd.hexdigest () #返回加密的数据def log (user,pwd): #登陆时候时候的函数, Since the MD5 cannot be reversed, the login is made with a positive solution with    open (' db ', ' R ', encoding= ' utf-8 ') as F: A for line in        F:            U,p=line.strip (). Split (' | ')            if u ==user and p = = MD5 (PWD): #登陆的时候验证用户名以及加密的密码跟之前保存的是否一样                return truedef Register (USER,PWD): # When registering the user name and encrypted password written into the file, save with    open (' db ', ' a ', encoding= ' utf-8 ') as f:        temp = user+ ' | ' +MD5 (PWD)        f.write ("temp") i=input (' 1 for login, 2 for registration: ') if i== ' 2 ':    user = input (' username: ')    pwd =input (' Password: ')    Register (USER,PWD) elif i== ' 1 ':    user = user = input (' User name: ')    pwd =input (' Password: ')    R=log (user,pwd) # Verify user name and password    if R ==true:        print (' login successful ')    else:        print (' login failed ') Else:    print (' Account not present ')

It simply writes a user's registration and login

Hashlib of common Python modules

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.