foxhole/app/httpsig.py

26 lines
758 B
Python

#!/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}")