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