封装一些用于加密的类
md5(), sha1.....
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> dir(hashlib)
[‘__all__‘, ‘__builtin_constructor_cache‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__get_builtin_constructor‘, ‘_
_loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘_hashlib‘, ‘algorithms_available‘, ‘algorithms_guaranteed‘, ‘blake2b‘, ‘bla
ke2s‘, ‘md5‘, ‘new‘, ‘pbkdf2_hmac‘, ‘scrypt‘, ‘sha1‘, ‘sha224‘, ‘sha256‘, ‘sha384‘, ‘sha3_224‘, ‘sha3_256‘, ‘sha3_384‘, ‘sha3_5
12‘, ‘sha512‘, ‘shake_128‘, ‘shake_256‘]
加密的目的: 用于判断和验证, 而并非解密
特点:
hashlib_demo.py
"""
MD5 加密算法:
给一个数据加密的三大步骤:
1. 获取一个加密对象
2. 使用加密对象的update方法进行加密, update方法可以调用多次
3. 通常通过hexdigest获取加密结果, 或者digest()方法
"""
import hashlib
# 获取一个加密对象(算法
m = hashlib.md5()
# 使用加密对象的update方法进行加密
m.update(‘abc中文‘.encode(‘utf-8‘))
m.update(‘def‘.encode(‘utf-8‘)) # 第二次加密是在第一个abc中文的加密上再次进行加密
# 通过hexdigest获取加密结果
# res = m.hexdigest() # 1af98e0571f7a24468a85f91b908d335
res = m.digest() # b‘/\x1bn)Nr\xd2Z\xe1\x96\xfeJ\xc2\xd2}\xe6‘
print(res)
# 给一个数据加密,
# 验证: 用另一个数据加密的结果和第一次加密的结果对比
# 如果结果相同, 说明原文相同
# 不同加密算法:实际上就是加密结果的长度不同
s = hashlib.sha224()
s.update(b‘abc‘)
print(len(s.hexdigest()))
print(len(hashlib.md5().hexdigest()))
(venv) C:\work\day16>python hashlib_demo.py
56
32
# 一个十六进制数等于多少个二进制数 ? 4个 16**1 = 2**4
所以为什么叫sha224呢?
答:56个十六进制等于224个二进制
同理 sha256也就是64个十六进制数 (hex英文是十六进制的意思)
print(len(hashlib.sha256().hexdigest()))
64
# 在创建加密对象时, 可以指定参数, 成为salt
m = hashlib.md5(b‘abc‘)
print(m.hexdigest())
m = hashlib.md5()
m.update(b‘abc‘)
print(m.hexdigest())
# 以下两个是一致的
m = hashlib.md5()
m.update(b‘abc‘)
m.update(b‘def‘)
print(m.hexdigest())
m = hashlib.md5()
m.update(b‘abcdef‘)
print(m.hexdigest())
(venv) C:\work\day16>python hashlib_demo.py
e80b5017098950fc58aad83c8c14978e
e80b5017098950fc58aad83c8c14978e
设计两个登陆和注册功能
# 注册, 登录程序:
def get_md5(username, password):
m = hashlib.md5()
m.update(username.encode(‘utf-8‘))
m.update(password.encode(‘utf-8‘))
return m.hexdigest()
def register(username, password):
# 加密
res = get_md5(username, password)
# 写入文件
with open(‘login‘, mode=‘at‘, encoding=‘utf-8‘) as f:
f.write(res)
f.write(‘\n‘)
def login(username, password):
# 获取当前登录信息的解密结果
res = get_md5(username, password)
# 读文件,和其中的数据进行对比
with open(‘login‘, mode=‘rt‘, encoding=‘utf-8‘) as f:
for line in f:
if res == line.strip():
return True
else:
return False
while True:
op = int(input(‘1.注册 2.登陆 3.退出‘))
if op == 3:
break
elif op == 1:
username = input(‘输入用户名:‘)
password = input(‘输入密码‘)
register(username, password)
elif op == 2:
username = input(‘输入用户名:‘)
password = input(‘输入密码‘)
res = login(username, password)
if res:
print(‘登陆成功‘)
else:
print(‘登录失败‘)
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登陆成功
1.注册 2.登陆 3.退出3
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出1
输入用户名:def
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:aaa
输入密码123342342
登录失败
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登录失败
1.注册 2.登陆 3.退出
Traceback (most recent call last):
File "hashlib_demo.py", line 74, in <module>
while True:
ValueError: invalid literal for int() with base 10: ‘‘
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出def
Traceback (most recent call last):
File "hashlib_demo.py", line 75, in <module>
op = int(input(‘1.注册 2.登陆 3.退出‘))
ValueError: invalid literal for int() with base 10: ‘def‘
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登陆成功
1.注册 2.登陆 3.退出3
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出1
输入用户名:def
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:aaa
输入密码123342342
登录失败
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登录失败
1.注册 2.登陆 3.退出
Traceback (most recent call last):
File "hashlib_demo.py", line 74, in <module>
while True:
ValueError: invalid literal for int() with base 10: ‘‘
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登陆成功
1.注册 2.登陆 3.退出3
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出1
输入用户名:def
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:aaa
输入密码123342342
登录失败
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登录失败
1.注册 2.登陆 3.退出
Traceback (most recent call last):
File "hashlib_demo.py", line 74, in <module>
while True:
ValueError: invalid literal for int() with base 10: ‘‘
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
Traceback (most recent call last):
File "hashlib_demo.py", line 85, in <module>
res = login(username, password)
File "hashlib_demo.py", line 68, in login
f.write(res)
io.UnsupportedOperation: not writable
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登陆成功
1.注册 2.登陆 3.退出3
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出1
输入用户名:def
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:aaa
输入密码123342342
登录失败
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登录失败
1.注册 2.登陆 3.退出
Traceback (most recent call last):
File "hashlib_demo.py", line 74, in <module>
while True:
ValueError: invalid literal for int() with base 10: ‘‘
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:abc
输入密码123
1.注册 2.登陆 3.退出def
Traceback (most recent call last):
File "hashlib_demo.py", line 75, in <module>
op = int(input(‘1.注册 2.登陆 3.退出‘))
ValueError: invalid literal for int() with base 10: ‘def‘
(venv) C:\work\day16>python hashlib_demo.py
1.注册 2.登陆 3.退出1
输入用户名:def
输入密码123
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123423423
登录失败
1.注册 2.登陆 3.退出2
输入用户名:abc
输入密码123
登陆成功
1.注册 2.登陆 3.退出
原文:https://www.cnblogs.com/sunnywillow/p/13251959.html