1 #!/usr/bin/env python
2 #coding=utf-8
3 __author__ = 'yinjia'
4
5 import hashlib
6 import sys
7 from project.day2 import PayrollSystem
8
9 #存储文件
10 filename = 'log.txt'
11 #账号信息表
12 result = {}
13
14 def md5(arg):
15 """
16 密码进行md5加密
17 :param arg: 用户的密码
18 :return: 返回进行加密后的密码
19 """
20 result = hashlib.md5(bytes("k1:1a2 k2:688",encoding='utf-8'))
21 result.update(bytes(arg,encoding='utf-8'))
22 return result.hexdigest()
23
24
25 def login(username,password):
26 """
27 用于验证用户密码
28 :param username:用户名
29 :param password:密码
30 :return: True,用户名验证成功;False,用户验证失败
31 """
32
33 with open(filename,"r") as f:
34 for line in f:
35 u,p = line.strip().split(":")
36 if username == u and md5(password) == p:
37 return True
38 return False
39
40 def register(username,password):
41 """
42 用户注册
43 :param username: 用户名
44 :param password: 密码
45 :return: True,用户注册成功;
46 """
47
48 with open(filename,"a") as f:
49 temp = "
" + username + ":" + md5(password)
50 f.write(temp)
51 return True
52
53 def user_exist(username):
54 """
55 检查用户名是否存在
56 :param username: 用户名
57 :return: True,用户名存在; False,用户名不存在
58 """
59 with open(filename,"r") as f:
60 for line in f:
61 line = line.strip()
62 line_list = line.split(":")
63 if username == line_list[0]:
64 return True
65 return False
66
67 def delete(username):
68 """
69 删除用户名
70 :param username: 用户名
71 :return: True,删除成功; False,删除失败
72 """
73 info()
74 del result[username]
75 with open(filename,'w') as f:
76 for key,value in result.items():
77 f.write("%s%s%s
" % (key,":",value))
78 return True
79
80 def edit(username,old_password,new_password):
81 """
82 修改密码
83 :param username:用户名
84 :param old_password:旧密码
85 :param new_password:新密码
86 :return:True,修改成功;False,修改失败
87 """
88 info()
89 ret = result[username]
90 if ret == md5(old_password):
91 result[username] = md5(new_password)
92 with open(filename, 'w') as f:
93 for key, value in result.items():
94 f.write("%s%s%s
" % (key, ":", value))
95 return True
96
97 def info():
98 """
99 提取账号信息
100 :return: 返回账号信息表
101 """
102 with open(filename,'r') as f:
103 for line in f:
104 line = line.strip()
105 result[line.split(":")[0]] = line.split(":")[1]
106 return result
107
108 def main():
109 info_msg = """
110 1、登录系统
111 2、注册账号
112 3、删除账号
113 4、修改密码
114 """
115
116 print('