fief_client.crypto
1import base64 2import hashlib 3import secrets 4 5 6def get_validation_hash(value: str) -> str: 7 """ 8 Return the validation hash of a value. 9 10 Useful to check the validity `c_hash` and `at_hash` claims. 11 """ 12 hasher = hashlib.sha256() 13 hasher.update(value.encode("utf-8")) 14 hash = hasher.digest() 15 16 half_hash = hash[0 : int(len(hash) / 2)] 17 # Remove the Base64 padding "==" at the end 18 base64_hash = base64.urlsafe_b64encode(half_hash)[:-2] 19 20 return base64_hash.decode("utf-8") 21 22 23def is_valid_hash(value: str, hash: str) -> bool: 24 """ 25 Check if a hash corresponds to the provided value. 26 27 Useful to check the validity `c_hash` and `at_hash` claims. 28 """ 29 value_hash = get_validation_hash(value) 30 return secrets.compare_digest(value_hash, hash)
def
get_validation_hash(value: str) -> str:
7def get_validation_hash(value: str) -> str: 8 """ 9 Return the validation hash of a value. 10 11 Useful to check the validity `c_hash` and `at_hash` claims. 12 """ 13 hasher = hashlib.sha256() 14 hasher.update(value.encode("utf-8")) 15 hash = hasher.digest() 16 17 half_hash = hash[0 : int(len(hash) / 2)] 18 # Remove the Base64 padding "==" at the end 19 base64_hash = base64.urlsafe_b64encode(half_hash)[:-2] 20 21 return base64_hash.decode("utf-8")
Return the validation hash of a value.
Useful to check the validity c_hash
and at_hash
claims.
def
is_valid_hash(value: str, hash: str) -> bool:
24def is_valid_hash(value: str, hash: str) -> bool: 25 """ 26 Check if a hash corresponds to the provided value. 27 28 Useful to check the validity `c_hash` and `at_hash` claims. 29 """ 30 value_hash = get_validation_hash(value) 31 return secrets.compare_digest(value_hash, hash)
Check if a hash corresponds to the provided value.
Useful to check the validity c_hash
and at_hash
claims.