一, importer hashlib
将任意长度字符串转换成定长的密文的字符串,字符串里的每个字符都是一个十六进制的数字,这个密文是不可逆的
算法: 特点: 在任何语言环境下,对于同一个字符串,用相同的算法,相同的手段去进行摘要,获得的值总是相同的
只要是不相同的字符串,得到得结果一定不同
种类: 包含 md5 算法 sha1 算法
md5 是一个算法,32位的字符串,每个字符都是一个十六进制, 效率快 算法相对简单,用的时间较久,市面上已经形成了撞库,
import hashlib ret = hashlib.md5(b'kajsbfkq') # 算出字符串的密文 print(ret.hexdigest()) # 动态加盐 username = input('username : ') passwd = input('password : ') md5obj = hashlib.md5(username.encode('utf-8')) #得到一个对象 md5obj.update(passwd.encode('utf-8')) #通过限制账号的不重叠,然后作为盐加到密码中 print(md5obj.hexdigest())
sha1 也是一个算法,40位的字符串,每个字符都是一个十六进制, 算法相对复杂 计算速度也慢
文件的一致性校验
# 小文件的一致性校验 md5_obj = hashlib.md5() with open('xx文件','rb') as f: md5_obj.update(f.read()) ret1 = md5_obj.hexdigest() md5_obj = hashlib.md5() with open('yy文件','rb') as f1: md5_obj.update(f1.read()) ret2 = md5_obj.hexdigest() print(ret1,ret2)
大文件一致性校验:
还没写,待插入
二,import configparser
#这是一个好多软件常见的配置文件 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no #用python生成器生成这样的文档 import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9', 'ForwardX11':'yes' } config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'} with open('example.ini', 'w') as configfile: config.write(configfile) #查找文件,理解成大字典,基于字典的形式 import configparser config = configparser.ConfigParser() print(config.sections()) # [] config.read('example.ini') print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] print('bytebong.com' in config) # False print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default会默认default的键 print(key) print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键 print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对 print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value #增删改操作 import configparser config = configparser.ConfigParser() config.read('example.ini') config.add_section('yuan') config.remove_section('bitbucket.org') config.remove_option('topsecret.server.com',"forwardx11") config.set('topsecret.server.com','k1','11111') config.set('yuan','k2','22222') config.write(open('new2.ini', "w"))
三,logging 模块