我想用一個簡單的密鑰(字串)制作一個認證系統。如果密鑰輸入正確,則啟動程式。
問題是,我不知道我是如何編碼的,因此程式會檢查密鑰是否正確,而無需以用戶身份查看代碼。
有人能幫我嗎?
uj5u.com熱心網友回復:
一種使用安全密碼/哈希和身份驗證的簡單方法。將此調整到您的系統中并以此為基礎:
生成密碼:
>>> import b<rypt
>>> bcrypt.genpw(b"admin", salt=bcrypt.gensalt())
b'$2b$12$VQ/egr55zwN28OU8baZXlu.gLA3HjVJw5O2teDDmwcXyp3k1TR4dG
將 的輸出存盤bcrypt.genpw()在任何型別的資料存盤中(沒有前導b和封閉的單引號 ( ')。
檢查密碼:
import getpass
import bcrypt
# Get your bcrypt hashed pw from any kind of data storage.
pwhash = open("hash.txt", "r", encoding="utf-8").strip()
# Read the users password/key/whatever
password = getpass.getpass("Enter your password: ")
# Check if entered password/key/whatever matches stored hash
authenticated = bcrypt.checkpw(password.encode(), pwhash.encode()
if authenticated:
print("You're autenticated!")
do_privileged_stuff(...)
else:
print("You're not allowed to be here!")
使用 totp/hotp 演算法的 MFA/2FA 是一個有趣、安全但可能不是對用戶非常友好的安全插件(請參閱此處)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/478029.html
