hashlib相关

把登录与注册的密码都换成密文形式

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() + '\n')

文件完整性校验(考虑大文件)

import hashlib
def copy():
    with open('db.txt','rb') as f1,\
        open('db1.txt','wb') as f2:
        f2.write(f1.read())

copy()

with open('db.txt','rb') as f:
    d = []
    a = f.read(10)
    while a:
        m = hashlib.md5(a)
        d.append(m.hexdigest())
        f.seek(10, 1)
        a = f.read(10)

print(d)
with open('db1.txt','rb') as f:
    d1 = []
    a = f.read(10)
    while a:
        m = hashlib.md5(a)
        d1.append(m.hexdigest())
        f.seek(10,1)
        a = f.read(10)
print(d1)
for i in range(len(d)):
    if d[i] != d1[i]:
        print('文件不完整')
        break
else:
    print('文件完整')

项目的配置文件采用configparser进行解析

import configparser as conf

settings = conf.ConfigParser()
settings.read('conf.txt')
print(settings.sections())
print(settings.items('section1'))
posted @ 2020-03-31 17:25  pythoner_wl  阅读(74)  评论(0编辑  收藏  举报