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