Python generates a hash hash--hashlib module

Source: Internet
Author: User

Python generates a hash hash--hashlib module

1) Generate hash and save in local

(1) Code

[Python] View plain copy
Import Hashlib
Import shelve

#原密码paw ={"Water": "123456", "root": "Admin123"}
#生成hash
M1 = HASHLIB.MD5 ("123456". Encode ("Utf-8"))
M2 = hashlib.md5 ("admin123". Encode ("Utf-8"))
H1 = M1.hexdigest ()
H2 = M2.hexdigest ()
Print (H1)
Print (H2)

#现在利用shelve保存账号和密码信息到本地
DB1 = Shelve.open ("E:/python_code/work/hash_dic")
db1["water"] = H1
db1["admin"] = H2
(2) Output

e10adc3949ba59abbe56e057f20f883e

0192023a7bbd73250516f069df18b500

2) Imitation landing

We've generated a hash of the password information above and saved it locally with the shelve library, and now it's time to use it.

(1) Code

[Python] View plain copy
Import Hashlib
Import shelve

#从shelve文件中读取用户信息
DB1 = Shelve.open ("E:/python_code/work/hash_dic")

#退出系统
Def Tuichu ():
Print ("Exiting system ...")
Exit ("Look forward to meeting you next!")

# action function, giving different permissions depending on user name
def Caozuo (user_name):
If user_name== "admin":
Tuichu () #测试用
Else
Tuichu () #测试用
#登录函数
def login ():
For I in range (3): #只有3次登录机会
user_name = input ("account number:")
User_pass = input ("Password:")
m = Hashlib.md5 (User_pass.encode ("Utf-8"))
Hash_pass = M.hexdigest ()
For Ku,vu in Db1.items ():
if user_name = = ku and Hash_pass = = Vu:
Print ("Login successful! You are welcome {}! ". Format (KU))
#这里可以执行操作函数
Caozuo (USER_NAME)
Break
Else
Print ("Wrong account or password!") You also have {} chance! ". Format (2-i))
Continue
Else
Print ("Today's login runs out!")
if __name__ = = ' __main__ ':
Login ()

Article: http://www.ylsjwang.com/dianshiju/24.html
(2) operation

Python generates a hash hash--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.