signing

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package signing signs HTTP requests with a shared key and verifies them.

A request signature is an HMAC-SHA256, keyed by a 32-byte hex key, over the method, the request URI as sent on the wire (escaped path and query), the Unix time, a random nonce, the body hash and an epoch. The verifier checks the signature, a time window, that the nonce is new and that the epoch is its own.

An epoch is random per verifier instance (a daemon start, a sharing listener), so a request captured by a program posing as the verifier is refused by the real one, whose epoch differs. A signer learns the epoch from a proof: it sends a fresh random challenge, and the verifier answers with its epoch and HMAC(key, "server", challenge, epoch). A signer sends signed requests only after checking such a proof, and checks the proof on replies too, so a listener without the key gets no signature at all. A whole reply is proved with HMAC(key, "server", challenge, epoch, status, body hash), so a relay cannot alter it either; WebSocket upgrades carry the short proof.

Every message is newline-joined parts that never contain a newline themselves, and each use has its own number of parts: a session value 2, a proof 3, a reply proof 5, a request signature 6. One kind of MAC can therefore never be taken for another.

Paired devices sign with their device key (package share); the CLI, MCP server and menu bar app sign requests to their own daemon with its API token (package server).

Index

Constants

View Source
const (
	// Window is the allowed difference between the signing and the
	// verifying clock.
	Window = 2 * time.Minute

	// MaxBody bounds a signed request body, which is read whole to be hashed.
	MaxBody = 8 << 20
)

Variables

View Source
var (
	HeaderDate      = Header("Date")
	HeaderNonce     = Header("Nonce")
	HeaderSignature = Header("Signature")
	// HeaderEpoch names the verifier instance a request is meant for;
	// signatures cover it. Replies carry it next to the proof.
	HeaderEpoch = Header("Epoch")
	// HeaderChallenge carries a signer's fresh challenge; HeaderProof the
	// verifier's answer to it.
	HeaderChallenge = Header("Challenge")
	HeaderProof     = Header("Proof")
)

Header names of a signed request.

View Source
var ErrClock = errors.New("the request time differs from this computer's clock by more than two minutes")

ErrClock means a request was signed outside the time window.

View Source
var ErrEpoch = errors.New("signed for another instance; ask for a new proof")

ErrEpoch means a request was signed for another instance of the verifier, such as a daemon that has restarted since.

View Source
var ErrReplay = errors.New("replayed request")

ErrReplay means a nonce was used before.

View Source
var ErrSignature = errors.New("bad signature")

ErrSignature means a signature is missing, malformed or wrong.

Functions

func BodyHash

func BodyHash(body []byte) string

BodyHash is the hex SHA-256 of a request body.

func Challenge

func Challenge(h http.Header) string

Challenge adds a fresh challenge to h and returns it.

func CheckEpoch

func CheckEpoch(h http.Header, epoch string) error

CheckEpoch reports ErrEpoch unless request header h names epoch.

func Header(name string) string

Header returns the name of a product header: "X-<product>-<name>".

func MAC

func MAC(key string, parts ...string) string

MAC returns the hex HMAC-SHA256 of parts joined by newlines, keyed by the hex key.

func NewEpoch

func NewEpoch() string

NewEpoch returns a random epoch for a verifier instance.

func Prove

func Prove(h, req http.Header, key, epoch string)

Prove answers the challenge of request header req in reply header h: the epoch and a proof that the replying side holds key. A malformed challenge gets no answer.

func ProveReply

func ProveReply(h, req http.Header, key, epoch string, status int, body []byte)

ProveReply answers the challenge of request header req in reply header h for a whole reply: the epoch and a proof over the challenge, the epoch, the status and the body. A malformed challenge gets no answer.

func Proven

func Proven(key, challenge string, h http.Header) (string, bool)

Proven reports whether reply header h proves that the replying side holds key, for challenge, and returns the epoch the proof vouches for.

func ProvenReply

func ProvenReply(key, challenge string, h http.Header, status int, body []byte) (string, bool)

ProvenReply reports whether reply header h proves, for challenge, that the replying side holds key and sent this status and body, and returns the epoch the proof vouches for.

func RandomHex

func RandomHex(n int) string

RandomHex returns n random bytes, hex encoded.

func Sign

func Sign(h http.Header, key, method, uri string, body []byte, epoch string)

Sign adds the signature headers for a request to h. uri is the request URI as sent on the wire; epoch is the one the verifier last proved.

func Signed

func Signed(h http.Header) bool

Signed reports whether a request carries a signature.

func ValidKey

func ValidKey(k string) bool

ValidKey reports whether k is a full 32-byte hex key; nothing else may sign or verify.

Types

type Verifier

type Verifier struct {
	// contains filtered or unexported fields
}

Verifier checks signed requests and remembers recent nonces to refuse replays. The zero value is ready to use.

func (*Verifier) Verify

func (v *Verifier) Verify(r *http.Request, key, uri string, body []byte) error

Verify checks a signed request against key. uri is the request URI as the signer sent it and body the whole request body. The epoch the signature covers is the request's epoch header; callers check it with CheckEpoch.

Jump to

Keyboard shortcuts

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