hashlib模块
算法介绍
Python 的hashlib提供了常见的摘要算法,如MD5,SHA1等。
摘要算法含义:
摘要算法又称哈希算法,散列算法。他通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示)
摘要算法之所以能指出数据是否被篡改过,就是因为摘要函数是一个单向函数,计算f(data)很容易,但通过digest反推data却非常困难。而且对原始数据做一个bit的修改,都会导致计算出的摘要完全不同。
我们以常见的摘要算法MD5为例,计算出一个字符串的MD5的值:
import hashlib
md5_obj = hashlib.md5()
s = input('>>>')
md5_obj.update(s.encode('utf-8'))
print(md5_obj.hexdigest()) #按提示输入以后得到输入字符计算后的加密字符串,如“584d396d6849ae653dd7b616d279b0fa”
如果数据量很大,可以分多次调用update(),最后的计算结果是一样的
import hashlib
md5_obj = hashlib.md5()
s = 'mypassword'
s2 = '3714'
md5_obj.update(s.encode('utf-8'))
print(md5_obj.hexdigest())
# 34819d7beeabb9260a5c854bc85b3e44
MD5是最常见的摘要算法,速度很快,生成结果是固定的128bit字节,通常用一个32位的16进制字符串表示。另一种常见的摘要算法是SHA1,调用SHA1和调用MD5完全类似。
>>> import hashlib
>>> sha1 = hashlib.sha1()
>>> sha1.update('this is my password,')
>>> sha1.update('pyhton sha1 password!')
>>> print sha1.hexdigest()
009dddccd8bdcfdcbe19a55bde8c129ede6ed9fb
SHA1的结果是160 bit字节,通常用一个40位的16进制字符串表示。比SHA1更安全的算法是SHA256和SHA512,不过越安全的算法越慢,而且摘要长度更长。
摘要算法应用
用户登录的用户名和口令通过算法加密存储到数据库中。
但是对于常用的MD5值很容易被计算出来,所以要确保存储的用户口令不是那些已经被计算出来的常用口令的MD5,这一方法通过UI原始口令加一个复杂字符串来实现,俗称“加盐”:
hashlib.md5('salt'.encode('utf-8'))
经过salt处理的口令,只要salt不被黑客知道,即使用户输入简单口令,也很难通过MD5反推明文口令。
但是如果两个用户都使用了相同的简单口令,如“123456”,在数据库中,将存储两条相同的MD5值,这说明这两个用户的口令是一样的。有没有办法让使用相同口令的用户存储不同的MD5呢?
如果假定用户无法修改登录名,就可以通过把登录名作为salt的一部分来计算MD5,从而实现相同口令的用户也存储不同的MD5.
摘要算法在很多地方都有广泛的应用。
configparser模块
该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键 = 值)
创建文件
来看一个好多软件的常见文档格式如下:
[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']['Host Port']) #50022
print(config['topsecret.server.com']['ForwardX11']) #no
print(config['bitbucket.org']) #<Section: bitbucket.org>
for key in config['bitbucket.org']:
print(key) #注意,有default会默认default的键
print(config.options('bitbucket.org'))
# ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(config.items('bitbucket.org'))
#[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
print(config.get('bitbucket.org','compression')) #yes
增删改操作
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'))
new2.ini文件内容如下:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes
[topsecret.server.com]
host port = 50022
k1 = 11111
[yuan]
k2 = 22222
logging模块
import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志,这说明默认的日志级别设置为WARNING(日志级别等级CRITICAL>ERROR>WARNING>INFO>DEBUG),默认的日志格式为日志级别:Logger名称:用户输出消息。