day014-python-hashlib加密 – Python量化投资

day014-python-hashlib加密

用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA512,MD5算法。

以下是算法示例:



 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import hashlib
 4 #=================MD5()加密=====================
 5 hhb = hashlib.md5(bytes("jahiuhfdakj", encoding='utf-8'))  #加盐处理
 6 ret = hhb.update(bytes("123", encoding='utf-8'))   #“123”是需要加密的字符串
 7 print("MD5加密:", hhb.hexdigest())
 8 
 9 
10 #=================SHA1()加密=====================
11 hhb = hashlib.sha1()
12 ret = hhb.update(bytes("123", encoding='utf-8'))
13 print("SHA1加密:", hhb.hexdigest())
14 
15 
16 #=================SHA224()加密=====================
17 hhb = hashlib.sha224()
18 ret = hhb.update(bytes("123", encoding='utf-8'))
19 print("SHA224加密:", hhb.hexdigest())
20 
21 #=================SHA256()加密=====================
22 hhb = hashlib.sha256()
23 ret = hhb.update(bytes("123", encoding='utf-8'))
24 print("SHA256加密:", hhb.hexdigest())
25 
26 #=================SHA512()加密=====================
27 hhb = hashlib.sha512()
28 ret = hhb.update(bytes("123", encoding='utf-8'))
29 print("SHA512加密:", hhb.hexdigest())
30 
31 #=================SHA3_256()加密=====================
32 hhb = hashlib.sha3_256()
33 ret = hhb.update(bytes("123", encoding='utf-8'))
34 print("SHA3_256加密:", hhb.hexdigest())
35 
36 #=================SHA3_384()加密=====================
37 hhb = hashlib.sha3_384()
38 ret = hhb.update(bytes("123", encoding='utf-8'))
39 print("SHA3_384加密:", hhb.hexdigest())
40 
41 #=================SHA3_512()加密=====================
42 hhb = hashlib.sha3_512()
43 ret = hhb.update(bytes("123", encoding='utf-8'))
44 print("SHA3_512加密:", hhb.hexdigest())



View Code

算法示例的结果:



1 MD5加密: 113ccbcda570622489c183f6a6e0121e
2 SHA1加密: 40bd001563085fc35165329ea1ff5c5ecbdbbeef
3 SHA224加密: 78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f
4 SHA256加密: a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
5 SHA512加密: 3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2
6 SHA3_256加密: a03ab19b866fc585b5cb1812a2f63ca861e7e7643ee5d43fd7106b623725fd67
7 SHA3_384加密: 9bd942d1678a25d029b114306f5e1dae49fe8abeeacd03cfab0f156aa2e363c988b1c12803d4a8c9ba38fdc873e5f007
8 SHA3_512加密: 48c8947f69c054a5caa934674ce8881d02bb18fb59d5a63eeaddff735b0e9801e87294783281ae49fc8287a0fd86779b27d7972d3e84f0fa0d826d7cb67dfefc



View Code

 

基于hashlib.md5()对密码进行加密实现用户登录验证的示例:



 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import hashlib
 5 #基于MD5对密码进行加密实现用户登录验证
 6 
 7 def md5(arg):
 8     """
 9     使用hashlib.md5()对密码进行加密处理
10     :param arg: 需要加密处理的密码
11     :return:
12     """
13     hash = hashlib.md5(bytes("我在学习python", encoding='utf-8'))  #加盐,使用密码加密更有保障
14     hash.update(bytes(arg, encoding='utf-8'))
15     return hash.hexdigest()
16 
17 def login(username, password):
18     """
19     用于用户登录验证
20     :param username: 用户名
21     :param password: 密码
22     :return: True,登录成功;False,登录失败。
23     """
24     with open("user.txt", "r", encoding="utf-8") as f:
25         for line in f:
26             line = line.strip()  #默认strip无参数,会去掉首尾空格、换行符;有参数则去除指定值
27             line_list = line.split("|")   #以|符号提取用户名和密码
28             if username == line_list[0] and md5(password) == line_list[1]:
29                 return True
30             else:
31                 return False
32 
33 def register(username, password):
34     """
35     用户注册
36     :param username:用户名
37     :param password:密码
38     :return: True,注册成功
39     """
40     with open("user.txt", "a", encoding="utf-8") as f:
41         temp = "\n" + username + "|" + md5(password)   #  "\n"换行符
42         f.write(temp)
43     return True
44 
45 def user_exsit(username):
46     """
47     注册时,判断用户名是否存在
48     :param username:用户名
49     :return:True, 用户名已存在
50     """
51     with open("user.txt", "r", encoding="utf-8") as f:
52         for line in f:
53             line = line.strip()
54             line_list = line.split("|")
55             if username == line_list[0]:
56                 return True
57         return False
58 
59 def main():
60     print("欢迎您使用本系统,请输入你进行操作选项。")
61     inp = input("1.登录;2.注册。请输入编号: ")
62     if inp == "1":
63         times = 1
64         while True:
65             if times == 4:
66                 print("输入3次用户名或密码不正确,请在一小时后再重试。")
67                 break
68             user = input("请输入你的用户名:")
69             pwd = input("请输入你的密码:")
70             is_login = login(user, pwd)
71             if is_login:
72                 print("恭喜您!系统登录成功。")
73                 break
74             else:
75                 print("用户名或密码不正确。")
76             times += 1
77     if inp == "2":
78         user = input("请输入你的用户名:")
79         pwd = input("请输入你的密码:")
80         if user_exsit(user):
81             print("用户名已经存在,注册失败!")
82         else:
83             ret = register(user, pwd)
84             if ret:
85                 print("注册成功!")
86             else:
87                 print("注册失败!")
88 
89 main()



View Code

加密后的user.txt的文件内容。

https://www.cnblogs.com/june-L/p/11628765.html

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论