GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python_test/test43.py at master · sinferwu/python_test

Expand file tree

/

test43.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
46 lines (34 loc) · 1.36 KB
/

test43.py

Copy path

File metadata and controls

46 lines (34 loc) · 1.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# -*- conding: utf-8 -*-
'练习内建模块之hmac'
__author__ = 'sergiojune'
import random, hmac # 这个模块是可以根据口令进行加密保存数据,相当于md5加slat的效果,还比他强
message = b'hello world'
key = b'key'
# 第一个和第二个参数都必须是bytes类型
p = hmac.new(key, message, digestmod='md5') # 指定md5算法,一个参数为口令,第二个为加密的信息
print(p.hexdigest())
# 作业:将上一节的salt改为标准的hmac算法,验证用户口令
def hmac_md5(key, s):
return hmac.new(key.encode('utf-8'), s.encode('utf-8'), 'MD5').hexdigest()
class User(object):
def __init__(self, username, password):
self.username = username
self.key = ''.join([chr(random.randint(48, 122)) for i in range(20)])
self.password = hmac_md5(self.key, password)
db = {
'michael': User('michael', '123456'),
'bob': User('bob', 'abc999'),
'alice': User('alice', 'alice2008')
}
def login(username, password):
user = db[username]
return user.password == hmac_md5(user.key, password)
# 测试:
assert login('michael', '123456')
assert login('bob', 'abc999')
assert login('alice', 'alice2008')
assert not login('michael', '1234567')
assert not login('bob', '123456')
assert not login('alice', 'Alice2008')
print('ok')
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.