forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0021.py
More file actions
32 lines (26 loc) · 944 Bytes
/
Copy path0021.py
File metadata and controls
32 lines (26 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''第 0021 题: 通常,登陆某个网站或者 APP,需要使用用户名和密码。密码是如何加密后存储起来的呢?请使用 Python 对密码加密。'''
__author__ = 'Drake-Z'
import hashlib
from collections import defaultdict
db = {}
db = defaultdict(lambda: 'N/A') #去掉登录可能产生的KeyError
def get_md5(password):
a = hashlib.md5()
a.update(password . encode('utf-8'))
return (a.hexdigest())
def register(username, password):
db[username] = get_md5(password + username + 'the-Salt')
def login(username, password):
b = get_md5(password + username + 'the-Salt' )
if b==db[username]:
return True
else:
return False
a = input('注册输入用户名:')
b = input('注册输入密码:')
register(a, b)
a = input('登录输入用户名:')
b = input('登录输入密码:')
print(login(a, b))