用於加密相關的操作,代替了md5模塊和sha模塊,主要提供SHA1,SHA224,SHA256,SHA512,MD5演算法。 以下是演算法示例: 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import hashlib 4 # MD5()加密 5 ...
用於加密相關的操作,代替了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的文件內容。