hmacutil

package
v1.353.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package hmacutil provides shared HMAC-SHA256 signing and verification utilities for Proxy A ↔ Proxy B communication.

Signature format:

HMAC-SHA256(secret, "METHOD\nPATH?QUERY\nTIMESTAMP\nBODY")

where TIMESTAMP is a Unix epoch in seconds (string), and BODY is the raw request body (may be empty). The resulting signature is sent as the X-Hub-Signature-256 header ("sha256=<hex>"), and the timestamp is sent separately as the X-Timestamp header.

Timestamp validation rejects requests whose timestamp deviates by more than MaxTimestampSkew from the server clock, preventing replay attacks.

Index

Constants

View Source
const (
	// TimestampHeader is the HTTP header name for the Unix timestamp included in signing.
	TimestampHeader = "X-Timestamp"

	// MaxTimestampSkew is the maximum allowed difference between the timestamp in the
	// request and the server's current time. Requests outside this window are rejected.
	MaxTimestampSkew = 5 * time.Minute
)

Variables

This section is empty.

Functions

func BuildMessage

func BuildMessage(method, pathWithQuery, timestamp string, body []byte) []byte

BuildMessage constructs the canonical signing message in the form:

METHOD\nPATH?QUERY\nTIMESTAMP\nBODY

pathWithQuery should be the full request URI including query string (e.g. "/api/v1/sessions?user_id=alice"). body may be nil or empty.

func NowTimestamp

func NowTimestamp() string

NowTimestamp returns the current Unix epoch as a decimal string.

func Sign

func Sign(secret, message []byte) string

Sign computes HMAC-SHA256 over message and returns the signature as "sha256=<hex>".

func ValidateTimestamp

func ValidateTimestamp(ts string) error

ValidateTimestamp parses ts as a Unix epoch (decimal string) and verifies that it falls within ±MaxTimestampSkew of the current time.

If ts is empty, the function returns an error – callers that need backward compatibility should handle the empty-string case themselves before calling ValidateTimestamp.

func Verify

func Verify(secret, message []byte, sig string) bool

Verify returns true if sig matches the expected HMAC-SHA256 signature of message. The comparison is constant-time to prevent timing attacks.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL