hashlib\logging\configparser
生活随笔
收集整理的這篇文章主要介紹了
hashlib\logging\configparser
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#!/usr/bin/env python
# -*- coding=utf-8 -*-import hashlib# hash:哈希算法,結(jié)果是內(nèi)存地址
# print(hash('123')) #每次都不一樣'''
hashlib模塊,與加密相關(guān),被稱作 摘要算法
什么是摘要算法呢?摘要算法又稱哈希算法、散列算法。它通過一個(gè)函數(shù),把任意長度的數(shù)據(jù)轉(zhuǎn)換為一個(gè)長度固定的數(shù)據(jù)串(通常用16進(jìn)制的字符串表示)。1、是一堆算法的合集,包含很多算法(加密的)2、hashlib的過程:將字符串--->數(shù)字的過程3、在不同的電腦上,hashlib對(duì)相同的字符串轉(zhuǎn)化成的數(shù)字相同
應(yīng)用場景:密文(密碼):將密碼用算法加密放置到數(shù)據(jù)庫,每次取出驗(yàn)證文件的校驗(yàn)
分類:md5:加密算法,常用算法,可以滿足一般的常用的需求sha:加密算法,級(jí)別高一些,數(shù)字越大級(jí)別越高,加密的效率越低,越安全# ---------- md5
s1 = '12343254'
ret = hashlib.md5() # 創(chuàng)建一個(gè)md5對(duì)象
ret.update(s1.encode('utf-8')) # 調(diào)用此update方法對(duì)參數(shù)進(jìn)行加密 bytes類型
print(ret.hexdigest()) # 得到加密后的結(jié)果 定長# 無論多少數(shù)據(jù),加密后的結(jié)果都是定長的
# 同一個(gè)數(shù)據(jù),但會(huì)的md5值相同
''''''
有一些黑客,將常用的密碼與對(duì)應(yīng)的md5值放到一個(gè)庫中,然后進(jìn)行撞庫
解決辦法:加鹽s2 = '12345'
ret = hashlib.md5('@$1*(^&@^2wqe'.encode('utf-8')) # 鹽 - @$1*(^&@^2wqe
ret.update(s2.encode('utf-8'))
print(ret.hexdigest())
''''''
#弊端:黑客如果盜取了固定鹽。。。。# 改進(jìn):變成隨機(jī)鹽 但是不能太隨機(jī),到時(shí)候不好匹配
# 賬號(hào)密碼:將賬號(hào)或者各種變形 設(shè)置成隨機(jī)鹽
username = 'alex'
password = '12345'
ret = hashlib.md5(username[::-1].encode('utf-8'))
ret.update(password.encode('utf-8'))
print(ret.hexdigest())
'''
'''
# --------------sha系列
# sha1 與 md5 級(jí)別相同,但是 sha1 比 md5 更安全一些
ret = hashlib.sha1()
ret.update('123456'.encode('utf-8'))
print(ret.hexdigest())# sha512 級(jí)別最高,效率低,安全性最大
ret = hashlib.sha512()
ret.update('1234'.encode('utf-8'))
print(ret.hexdigest())
''''''
# ------------ 文件的校驗(yàn)
# 對(duì)于小文件可以,但是超大的文件內(nèi)存受不了
# 通過校驗(yàn)前后文件編碼過后的內(nèi)容,可以查看文件有沒有丟失def func(file_name):with open(file_name,mode='rb') as f1:ret = hashlib.md5()ret.update(f1.read())return ret.hexdigest()
print(func('hash_exec'))
'''
'''
# 校驗(yàn)的時(shí)候也可以分段校驗(yàn)
# 分段的時(shí)候切記里面的任何一個(gè)字符都不要落下
s1 = 'I am 旭哥, 都別惹我.... 不服你試試'
ret = hashlib.md5()
ret.update(s1.encode('utf-8'))
print(ret.hexdigest()) # 15f614e4f03312320cc5cf83c8b2706fs1 = 'I am 旭哥, 都別惹我.... 不服你試試'
ret = hashlib.md5()
ret.update('I am'.encode('utf-8'))
ret.update(' 旭哥, '.encode('utf-8'))
ret.update('都別惹我....'.encode('utf-8'))
ret.update(' 不服你試試'.encode('utf-8'))
print(ret.hexdigest()) # 15f614e4f03312320cc5cf83c8b2706f
'''
#大文件 - 一次校驗(yàn)一部分
def func(file_name):with open(file_name,'rb') as f1:ret = hashlib.md5()while True:content = f1.read(1024)if content: # 檢驗(yàn)文件內(nèi)容是否為空
ret.update(content)else:breakreturn ret.hexdigest() hashlib #!/usr/bin/env python
# -*- coding=utf-8 -*-
# 配置文件:放置一些常用的變量,路徑.
# 幫助你操作(創(chuàng)建,增,刪,改,查)一個(gè)配置文件import configparser'''
# 創(chuàng)建一個(gè)配置文件
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('配置文件.ini','w') as configfile:config.write(configfile)
''''''
#基于字典形式,查找文件內(nèi)容
config = configparser.ConfigParser()
config.read('配置文件.ini')
print(config.sections()) #沒有DEFAULT,它是特殊的,可以看做一個(gè)全局的。
print('111'in config) # False 判斷節(jié)名是否在配置文件中#對(duì)配置文件中的節(jié)對(duì)應(yīng)的項(xiàng),取值
print(config['bitbucket.org']['user']) # hg
print(config['bitbucket.org']) # <Section: bitbucket.org> 可迭代對(duì)象for key in config['bitbucket.org']:print(key) #注意:有default會(huì)默認(rèn)default里面節(jié)的值print(config.options('bitbucket.org')) #找到default和bitbucket.org下的鍵
print(config.items('bitbucket.org')) #找到default和bitbucket.org下的鍵值對(duì)print(config.get('bitbucket.org','compression')) #yes 判斷節(jié)點(diǎn)和項(xiàng)是否匹配'''#增刪改
config = configparser.ConfigParser()
config.read('配置文件.ini')
config.add_section('alex')
config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11")config.set('alex','k1','w')
config.write(open('配置文件2.ini','w')) 配置文件 #!/usr/bin/env python
# -*- coding=utf-8 -*-
# 配置文件:放置一些常用的變量,路徑.
# 幫助你操作(創(chuàng)建,增,刪,改,查)一個(gè)配置文件import configparser'''
# 創(chuàng)建一個(gè)配置文件
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('配置文件.ini','w') as configfile:config.write(configfile)
''''''
#基于字典形式,查找文件內(nèi)容
config = configparser.ConfigParser()
config.read('配置文件.ini')
print(config.sections()) #沒有DEFAULT,它是特殊的,可以看做一個(gè)全局的。
print('111'in config) # False 判斷節(jié)名是否在配置文件中#對(duì)配置文件中的節(jié)對(duì)應(yīng)的項(xiàng),取值
print(config['bitbucket.org']['user']) # hg
print(config['bitbucket.org']) # <Section: bitbucket.org> 可迭代對(duì)象for key in config['bitbucket.org']:print(key) #注意:有default會(huì)默認(rèn)default里面節(jié)的值print(config.options('bitbucket.org')) #找到default和bitbucket.org下的鍵
print(config.items('bitbucket.org')) #找到default和bitbucket.org下的鍵值對(duì)print(config.get('bitbucket.org','compression')) #yes 判斷節(jié)點(diǎn)和項(xiàng)是否匹配'''#增刪改
config = configparser.ConfigParser()
config.read('配置文件.ini')
config.add_section('alex')
config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11")config.set('alex','k1','w')
config.write(open('配置文件2.ini','w')) logging—低配版
?
轉(zhuǎn)載于:https://www.cnblogs.com/liangying666/p/9282362.html
總結(jié)
以上是生活随笔為你收集整理的hashlib\logging\configparser的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Memory Limits for Wi
- 下一篇: numpy 和tensorflow中ar