canonicalize

package
v0.0.0-...-278c5aa Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package canonicalize provides RFC 8785 (JSON Canonicalization Scheme) compliant serialization for deterministic hashing of HELM artifacts. quantum_posture: canonical bytes bind hashes and signatures; this encoder does not itself choose a classical, hybrid, or post-quantum signing profile.

Index

Constants

View Source
const MaxSafeInteger = 9007199254740991

MaxSafeInteger is 2^53-1, the largest integer an IEEE 754 double represents exactly. RFC 8785 Appendix B recommends confining true integers to ±MaxSafeInteger for maximum compliance with the ECMAScript JSON object.

Variables

View Source
var ErrNonInteroperableNumber = errors.New("canonicalize: number outside the interoperable subset")

ErrNonInteroperableNumber is returned by CheckInteroperableNumbers for a value whose JSON number literal is not guaranteed to be byte-identical between HELM canonical JSON and a strict RFC 8785 implementation.

Functions

func CanonicalHash

func CanonicalHash(v interface{}) (string, error)

CanonicalHash returns the SHA-256 hex digest of the canonical JSON representation of v.

func Canonicalize

func Canonicalize(schemaID string, raw interface{}) (*interfaces.Artifact, error)

Canonicalize converts a raw value into a canonical Artifact. It detects the content type and applies the appropriate canonicalization logic.

func CheckInteroperableNumbers

func CheckInteroperableNumbers(v interface{}) error

CheckInteroperableNumbers reports whether every number reachable from v is inside the subset on which HELM canonical JSON and RFC 8785 produce identical bytes: an integer in [-MaxSafeInteger, MaxSafeInteger] written without a sign on zero, without leading zeros, without a fraction and without an exponent.

Numbers outside that subset are not rejected by JCS — JCS preserves the literal — so this is the gate every signed artifact and every published test vector MUST pass before its bytes are frozen. Without it, "1e2", "1.0", "-0" and 9007199254740993 canonicalize to literals a conformant RFC 8785 implementation will not reproduce.

The returned error names the JSON path of the first offending value.

func ComputeArtifactHash

func ComputeArtifactHash(data []byte) string

ComputeArtifactHash returns the SHA-256 multihash of the canonical bytes.

func HashBytes

func HashBytes(data []byte) string

HashBytes computes SHA-256 hash of raw bytes and returns hex string

func InteroperableJCS

func InteroperableJCS(v interface{}) ([]byte, error)

InteroperableJCS is JCS gated on CheckInteroperableNumbers. Use it to mint canonical bytes that a third-party RFC 8785 implementation reproduces.

func JCS

func JCS(v interface{}) ([]byte, error)

JCS returns the RFC 8785 canonical JSON representation of v.

Conformance and the one deviation, stated precisely (see protocols/specs/rfc/canonical-json-v1.md):

  1. Object keys are sorted by UTF-16 code unit, per RFC 8785 Section 3.2.3.
  2. Strings use the RFC 8785 Section 3.2.2.2 escape set: only U+0000..U+001F, '"' and '\' are escaped. HTML escaping is DISABLED; U+2028/U+2029 and U+007F are emitted as literal UTF-8.
  3. DEVIATION from RFC 8785 Section 3.2.2.3: numbers are emitted as the JSON literal produced by encoding/json for v (or, for a value decoded with json.Number, the literal from the source document) rather than by the ECMAScript Number-to-String algorithm. Over the interoperable subset enforced by CheckInteroperableNumbers the two agree byte for byte; outside it they do not. Signed HELM artifacts are confined to that subset.

JCS is lossy on invalid UTF-8: the encoding/json pre-marshal replaces ill-formed byte sequences with U+FFFD before this function sees them, so invalid input canonicalizes rather than failing. Callers that must reject ill-formed input have to validate before calling.

func JCSString

func JCSString(v interface{}) (string, error)

JCSString returns the JCS canonical form as a string

Types

This section is empty.

Jump to

Keyboard shortcuts

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