python密碼學各種加密模塊教程
在本章中,您將詳細了解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)建其哈希值.
它還包括用于驗證密碼以進行身份驗證的邏輯.
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ù)之前,將隨機序列添加到密碼字符串中.
以上就是python密碼學各種加密模塊教程的詳細內(nèi)容,更多關(guān)于Python密碼學加密模塊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python獲取外網(wǎng)ip地址的方法總結(jié)
這篇文章主要介紹了python獲取外網(wǎng)ip地址的方法,實例總結(jié)了四種常用的獲取外網(wǎng)IP地址的技巧,需要的朋友可以參考下2015-07-07
python并發(fā)爬蟲實用工具tomorrow實用解析
這篇文章主要介紹了python并發(fā)爬蟲實用工具tomorrow實用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
Python基于文本內(nèi)容實現(xiàn)隱私信息提取與評估
這篇文章主要為大家介紹了Python如何實現(xiàn)基于文本內(nèi)容的用戶隱私泄露風險評估系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-03-03

