emmm......就当练习了系列18
Posted lucky-cat233
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了emmm......就当练习了系列18相关的知识,希望对你有一定的参考价值。
1、把登录与注册的密码都换成密文形式
import hashlib
pwd = input(‘请输入密码:‘).strip()
with open(‘db.txt‘,‘a‘,encoding=‘utf-8‘) as f:
hash1 = hashlib.md5(pwd.encode(‘utf-8‘))
print(hash1.hexdigest(),type(hash1.hexdigest()))
f.write(hash1.hexdigest() + ‘
‘)
2、文件完整性校验(考虑大文件)
import hashlib
import random
import os
def get_md5(file_path):
size=os.path.getsize(file_path)
bytes_size=size*8
list_seek=[int(bytes_size/4),int(bytes_size/3),int(bytes_size/2)]
with open(file_path,"rb") as f:
m1=hashlib.md5()
for i in range(3):
f.seek(i,0)
data=f.read(7)
m1.update(data)
res=m1.hexdigest()
return res
# res=get_md5(r"E:oldboyclasswork
eader_system
eadme.txt")
# print(res) #a5d2db97ffa024feeb87b56b9410eca7
#将文件改变前的md5存下来
res="a5d2db97ffa024feeb87b56b9410eca7"
def change_file(file_path):
size=os.path.getsize(file_path)
# print(size)
bytes_size=size*8
list_seek=[int(bytes_size/4),int(bytes_size/3),int(bytes_size/2)]
with open(file_path,"rb") as f:
m1=hashlib.md5()
for i in range(3):
f.seek(i,0)
data=f.read(7)
m1.update(data)
result=m1.hexdigest()
print(result)
if result == res:
print("文件完整")
else:
print("文件已损坏")
change_file(r"E:oldboyclasswork
eader_system
eadme.txt")
3、注册功能改用json实现
import json,hashlib
def register():
user = input(‘请输入账号:‘).strip()
pwd = input(‘请输入密码:‘).strip()
pwd1 = input(‘请确认密码:‘).strip()
if pwd == pwd1:
import hashlib
m = hashlib.md5(pwd.encode(‘utf-8‘))
m = m.hexdigest()
with open(‘db.txt‘,‘a‘,encoding=‘utf-8‘) as f:
json.dump(‘{}:{}:{}‘.format(user,m,0),f)
print(‘注册成功‘)
else:
print(‘密码不一致‘)
register()
4、项目的配置文件采用configparser进行解析
import configparser
config=configparser.ConfigParser()
config.read(settings.py)
print(config.sections())
print(config.options(‘section1‘))
print(config.items(‘section1‘))
以上是关于emmm......就当练习了系列18的主要内容,如果未能解决你的问题,请参考以下文章