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