feat/add digest calculate

This commit is contained in:
SouthFox 2023-03-16 16:28:35 +08:00 committed by SouthFox
parent a379706f78
commit ec099b76ec

26
app/httpsig.py Normal file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import base64
from typing import Literal, TypedDict, cast, Any
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5
class HttpSignature:
"""
calculation and verification of HTTP signatures
"""
@classmethod
def calculation_digest(cls,
body: bytes,
algorithm="sha-256")-> str :
"""
Calculates the digest header value for a given HTTP body
"""
if "sha-256" == algorithm:
h = SHA256.new()
h.update(body)
return "SHA-256=" + \
base64.b64encode(h.digest()).decode("utf-8")
else:
raise ValueError(f"Not support algorithm {algorithm}")