blob: b0c9989b4236d1c338d55eb8603ee33154ab4039 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import base64
import hashlib
from urllib.parse import urlencode
import bcrypt
from fastapi import FastAPI
def bcrypt_sha256_hash(password: str) -> str:
digest = hashlib.sha256(password.encode("utf-8")).digest()
encoded = base64.b64encode(digest)
return bcrypt.hashpw(encoded, bcrypt.gensalt()).decode("utf-8")
def bcrypt_sha256_verify(password: str, hashed: str) -> bool:
digest = hashlib.sha256(password.encode("utf-8")).digest()
encoded = base64.b64encode(digest)
return bcrypt.checkpw(encoded, hashed.encode("utf-8"))
def get_url_path_with_query(
app: FastAPI, name: str, params: dict | None = None, /, **path_params: str
):
base = app.url_path_for(name, **path_params)
return base + "?" + urlencode(params) if params is not None else base
|