最后活跃于 8 months ago

用 bcrypt 進行密碼加密與驗證,支援 salt、自動安全雜湊,保護你的使用者資料不被爆破!適合登入驗證與帳號系統 🚀

修订 760598201beb8fda4e6821440da22e84bb532f13

bcrypt_util.py 原始文件
1import bcrypt
2
3
4def encode_password(password: str) -> str:
5 salt = bcrypt.gensalt()
6 hashed = bcrypt.hashpw(password.encode(), salt)
7 return hashed.decode()
8
9
10def check_password(password: str, hashed: str) -> bool:
11 return bcrypt.checkpw(password.encode(), hashed.encode())
12
13
14if __name__ == "__main__":
15 password = "super secret password"
16 hashed = encode_password(password)
17 print(hashed)
18 print(check_password(password, hashed))
19