python密碼學(xué)各種加密模塊教程
在本章中,您將詳細(xì)了解Python中各種加密模塊.
加密模塊
它包含所有配方和基元,并在Python中提供高級編碼接口.您可以使用以下命令安裝加密模塊 :
pip install cryptography
代碼
您可以使用以下代碼實現(xiàn)加密模塊 :
from?cryptography.fernet?import?Fernet key?=?Fernet.generate_key() cipher_suite?=?Fernet(key) cipher_text?=?cipher_suite.encrypt("This?example?is?used?to?demonstrate?cryptography?module") plain_text?=?cipher_suite.decrypt(cipher_text)
輸出
上面給出的代碼產(chǎn)生以下輸出 :
此處給出的代碼用于驗證密碼并創(chuàng)建其哈希值.
它還包括用于驗證密碼以進(jìn)行身份驗證的邏輯.
import?uuid import?hashlib def?hash_password(password): ???#?uuid?is?used?to?generate?a?random?number?of?the?specified?password ???salt?=?uuid.uuid4().hex ???return?hashlib.sha256(salt.encode()?+?password.encode()).hexdigest()?+?':'?+?salt def?check_password(hashed_password,?user_password): ???password,?salt?=?hashed_password.split(':') ???return?password?==?hashlib.sha256(salt.encode()?+?user_password.encode()).hexdigest() new_pass?=?input('Please?enter?a?password:?') hashed_password?=?hash_password(new_pass) print('The?string?to?store?in?the?db?is:?'?+?hashed_password) old_pass?=?input('Now?please?enter?the?password?again?to?check:?') if?check_password(hashed_password,?old_pass): ???print('You?entered?the?right?password') else: ???print('Passwords?do?not?match')
輸出
場景1 : 如果您輸入了正確的密碼,您可以找到以下輸出 :
情景2 : 如果我們輸入錯誤的密碼,您可以找到以下輸出 :
說明
Hashlib 包用于在數(shù)據(jù)庫中存儲密碼.在此程序中,使用 salt ,在實現(xiàn)哈希函數(shù)之前,將隨機(jī)序列添加到密碼字符串中.
以上就是python密碼學(xué)各種加密模塊教程的詳細(xì)內(nèi)容,更多關(guān)于Python密碼學(xué)加密模塊的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python?hashlib模塊與哈希算法保護(hù)數(shù)據(jù)完整性教程
- Python基礎(chǔ)之hashlib模塊subprocess模塊logging模塊
- Python?HMAC模塊維護(hù)數(shù)據(jù)安全技術(shù)實例探索
- python借助ChatGPT讀取.env實現(xiàn)文件配置隔離保障私有數(shù)據(jù)安全
- Python3.10耙梳加密算法Encryption種類及開發(fā)場景
- python密碼學(xué)RSA密碼加密教程
- python密碼學(xué)實現(xiàn)文件加密教程
- Python hashlib庫數(shù)據(jù)安全加密必備指南
相關(guān)文章
Python 數(shù)據(jù)可視化之Seaborn詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化庫seaborn的使用總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-11-11python編寫softmax函數(shù)、交叉熵函數(shù)實例
這篇文章主要介紹了python編寫softmax函數(shù)、交叉熵函數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python中的split()函數(shù)和os.path.split()函數(shù)使用詳解
今天小編就為大家分享一篇python中的split()函數(shù)和os.path.split()函數(shù)使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12python 矢量數(shù)據(jù)轉(zhuǎn)柵格數(shù)據(jù)代碼實例
這篇文章主要介紹了python 矢量數(shù)據(jù)轉(zhuǎn)柵格數(shù)據(jù)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09