码迷,mamicode.com
首页 > 其他好文 > 详细

emmm......就当练习了系列18

时间:2020-03-31 22:37:00      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:完整   rip   大文件   ons   range   ==   形式   get   coding   

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

 


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:\oldboy\classwork\reader_system\readme.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:\oldboy\classwork\reader_system\readme.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

标签:完整   rip   大文件   ons   range   ==   形式   get   coding   

原文地址:https://www.cnblogs.com/lucky-cat233/p/12609044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!