import hashlib 加密算法相关
hashlib 中包含许多加密算法,不一一列举,用法大同小异
hashlib.md5()
:利用MD5进行加密
import hashlib
ss = 'Hello World' # 需要加密的字符串
md5 = hashlib.md5() # 定义一个MD5对象
md5.update(ss.encode('utf-8')) # 利用MD5对象加密字符串(需要对指定字符串编码)
print(md5.hexdigest()) # 打印密文(十六进制)
===================================
b10a8db164e0754105b7a99be72e3fe5
hashlib.sha1()
:利用SHA1进行加密
import hashlib
ss = 'Hello Word' # 需要加密的字符串
sha1 = hashlib.sha1(ss.encode('utf-8')) # 利用SHA1加密
print(sha1.hexdigest()) # 打印密文(十六进制)
===================================
44d3a83db52a0cf0f908ad0ef55c83f4771f66ad
hashlib.sha256()
:利用SHA256进行加密
import hashlib
ss = 'Hello Word' # 需要加密的字符串
sha256 = hashlib.sha256(ss.encode('utf-8')) # 利用SHA256加密
print(sha256.hexdigest()) # 打印密文(十六进制)
===================================
9aea7eb32aebe6f10c3ec1381c66e18ffd50f20c54e29fd85b5ae159cf80c9d0
利用 hashlib
中的方法对用户信息加密存储与验证(不加盐)
import csv
import hashlib
def register():
username = input('UserName:')
password = input('PassWord:')
user = []
user.append(username)
password = hashlib.sha256(password.encode('utf-8')).hexdigest()
user.append(password)
with open('userbook.csv','a+',newline='') as ws:
csv_ws = csv.writer(ws)
csv_ws.writerow(user)
print('Regist Successful')
def checker():
username = input('UserName:')
password = input('PassWord:')
password = hashlib.sha256(password.encode('utf-8')).hexdigest()
with open('userbook.csv','r') as rs:
csv_rs = csv.reader(rs)
for user in csv_rs:
if user[0] == username and user[1] == password:
print('Successful')
break
else:
print('Error')
if __name__ == '__main__':
register()
checker()
利用 hashlib
中的方法对用户信息加密存储与验证(加盐)
import csv
import hashlib
def register():
username = input('UserName:')
password = input('PassWord:')
user = []
user.append(username)
sha256 = hashlib.sha256(b'salt') # 实例化对象时给一个参数,这个参数叫做盐
sha256.update(password.encode('utf-8'))
password = sha256.hexdigest()
user.append(password)
with open('userbook.csv','a+',newline='') as ws:
csv_ws = csv.writer(ws)
csv_ws.writerow(user)
print('Regist Successful')
def checker():
username = input('UserName:')
password = input('PassWord:')
sha256 = hashlib.sha256(b'salt') # 解密时需要加同样的盐
sha256.update(password.encode('utf-8'))
password = sha256.hexdigest()
with open('userbook.csv','r') as rs:
csv_rs = csv.reader(rs)
for user in csv_rs:
if user[0] == username and user[1] == password:
print('Successful')
break
else:
print('Error')
if __name__ == '__main__':
register()
checker()
import logging 日志相关
日志级别
CRITICAL
:严重警告,出现该类警告代表错误可能导致程序崩溃
FATAL
:致命错误,出现该类警告代表严重的错误事件可能导致程序退出
ERROR
:错误,程序的报错信息,虽然发生但不影响正常运行
WARN/WARNING
:警告,潜在错误
INFO
:一般事件信息,可以用于确认程序工作正常
DEBUG
:调试,追踪问题用
NOTSET
:不设置
使用日志
import logging
# 实例化一个日志对象
logger = logging.getLogger()
# 设置级别
logger.setLevel(logging.ERROR)
# 创建一个handler对象,需要给定具体文件
file = 'log/log.txt'
handler = logging.FileHandler(file)
handler.setLevel(logging.ERROR)
# 创建一个formatter格式对象
fmt = logging.Formatter('%(asctime)s - %(module)s - ' +
'%(filename)s[%(lineno)d] - %(levelname)s:%(message)s')
handler.setFormatter(fmt)
# 将handler交给logger管理
logger.addHandler(handler)
# 定义一个能够引起错误的函数
def func():
try:
num = int('xyz') # 该字符串不能被强转成int
for i in range(num):
print(i,end=' ')
except:
logger.error('NOT a Number') # 调用日志进行记录
finally:
print('------end------')
func() # 调用该函数