Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.7 KB
Raw Blame
import hmac
import base64
import hashlib
from typing import Literal


def encode_digest(encoding: Literal["hex", "base64", "base64url", "binary"], digest: bytes) -> str:
    if encoding == "hex":
        return digest.hex()
    elif encoding == "base64":
        return base64.b64encode(digest).decode()
    elif encoding == "base64url":
        return base64.urlsafe_b64encode(digest).decode().rstrip("=")
    elif encoding == "binary":
        return digest.decode("latin1")


def md5(data: str | None, encoding: Literal["hex", "base64", "base64url", "binary"] = "hex") -> str | None:
    if data is None:
        return None
    # nosemgrep: python.lang.security.insecure-hash-algorithms-md5.insecure-hash-algorithm-md5
    digest = hashlib.md5(data.encode()).digest()

    return encode_digest(encoding, digest)


def sha256(data: str | None, encoding: Literal["hex", "base64", "base64url", "binary"] = "hex") -> str | None:
    if data is None:
        return None
    digest = hashlib.sha256(data.encode()).digest()

    return encode_digest(encoding, digest)


def sha1(data: str | None, encoding: Literal["hex", "base64", "base64url", "binary"] = "hex") -> str | None:
    if data is None:
        return None
    # SHA-1 is only here because vendors sign webhooks with it. Never pick it for new work.
    # nosemgrep: python.lang.security.insecure-hash-algorithms.insecure-hash-algorithm-sha1
    digest = hashlib.sha1(data.encode()).digest()

    return encode_digest(encoding, digest)


def _hmac_chain(data: list, digestmod: str, encoding: Literal["hex", "base64", "base64url", "binary"]) -> str:
    if len(data) < 2:
        raise ValueError("Data array must contain at least two elements.")

    hmac_obj = hmac.new(data[0].encode(), data[1].encode(), digestmod)
    for i in range(2, len(data)):
        hmac_obj = hmac.new(hmac_obj.digest(), data[i].encode(), digestmod)

    digest = hmac_obj.digest()

    return encode_digest(encoding, digest)


def sha256HmacChain(data: list, encoding: Literal["hex", "base64", "base64url", "binary"] = "hex") -> str:
    return _hmac_chain(data, "sha256", encoding)


def sha1HmacChain(data: list, encoding: Literal["hex", "base64", "base64url", "binary"] = "hex") -> str:
    return _hmac_chain(data, "sha1", encoding)


def sha256HmacBase64Key(key: str, data: str, encoding: Literal["hex", "base64", "base64url", "binary"] = "hex") -> str:
    # Svix-style webhook secrets are random bytes, which a string key would corrupt. The extra
    # padding makes the decode accept a key with or without padding, as Node does.
    key_bytes = base64.b64decode(key.strip() + "==")
    digest = hmac.new(key_bytes, data.encode(), hashlib.sha256).digest()

    return encode_digest(encoding, digest)