A compact, deterministic serialization and AES-GCM encryption system designed for predictable, schema-stable payload handling across services. The library provides:
- Typed binary serialization
- Optional compression
- AES‑256‑GCM encryption with HKDF key derivation
- Key rotation via KID (Key Identifier)
- Schema hashing for payload integrity
- Base64 or binary output formats
- Protocol spec (draft):
docs/protocol.md - Threat model (draft):
docs/threat-model.md - Key lifecycle policy (draft):
docs/key-lifecycle-policy.md - Key rotation playbook (draft):
docs/key-rotation-playbook.md - Compatibility policy (draft):
docs/compatibility-policy.md - Deprecation policy (draft):
docs/deprecation-policy.md - Interoperability guide (draft):
docs/interoperability.md - Release/provenance process (draft):
docs/release-process.md - SLO targets (draft):
docs/slo-targets.md - Branch protection checklist:
docs/branch-protection-required-checks.md - Independent security review plan:
docs/independent-security-review.md - Security disclosure policy:
SECURITY.md
-
Converts JS objects into a stable binary format.
-
Supports:
nullbooleannumber(int32 + float64)bigint(int64)stringDateUint8Array- Nested objects and arrays.
-
Produces a schema hash (SHA‑256 of
key:typeTag) to ensure structural consistency. -
Decoder can optionally accept legacy schema-hash format for compatibility.
-
Uses AES‑GCM with 96‑bit IV.
-
HKDF(SHA‑256) used to derive a strong encryption key.
-
Output layout:
[flag|version][kid][iv][tag][ciphertext]
-
Supports key rotation via
kid. -
Supports
binaryorbase64output.
- Uses the KID byte to select the correct key.
- Verifies authentication via AES‑GCM tag.
- Supports optional decompression.
- Uses
deflateSync/inflateSync. - Reduces serialized payload sizes before encryption.
- Deep validation of values before serialization.
- Prevents unsupported types from passing silently.
Encrypts and serializes a payload.
Options:
key: string— raw key materialcompress?: boolean— enables compressionformat?: "binary" | "base64"— output formatkid?: number— key identifier (defaults to 0)aad?: string | Uint8Array— optional authenticated associated data (must match on decrypt)logger?: (event) => void— optional structured log hook (metadata only, no payload/key data)
Returns:
ArrayBufferorstring
Decrypts and deserializes a payload.
Options:
keyMap: Record<number, string>— key lookup table by KIDcompress?: boolean— whether the original payload was compressedallowLegacySchemaHash?: boolean— defaults totrue; setfalsefor strict schema-hash enforcementaad?: string | Uint8Array— optional authenticated associated datalogger?: (event) => void— optional structured log hook
Returns:
Record<string, unknown>
Creates a deterministic binary representation.
Parses a serialized buffer back into an object.
Ensures a JS value can be serialized.
import { encryptPayload } from "binx-core";
const encrypted = encryptPayload(
{ userId: 42, active: true, when: new Date() },
{ key: "secret", compress: true, format: "base64", kid: 1 }
);import { decryptPayload } from "binx-core";
const payload = decryptPayload(encrypted, {
keyMap: { 1: "secret" },
compress: true,
});[ 1 byte flag+version ]
[ 1 byte KID ]
[ 12 bytes IV ]
[ 16 bytes GCM tag ]
[ ciphertext ... ]
[version][fieldCount][schemaHash32]
repeat for each key:
[keyLen][keyBytes][tag][valueLen][valueBytes...]
- Multiple keys supported via
keyMap. - Sender attaches
kid. - Receiver resolves via
keyMap[kid].
- Throws on unsupported schema version.
- Throws on invalid KID.
- Throws on AES‑GCM authentication failure.
- Throws on non‑serializable values.
Secure usage patterns:
- Store keys in a secret manager or KMS, not in source code.
- Rotate keys regularly and use KID-based migration windows.
- Enable strict schema mode in higher-assurance contexts by setting:
allowLegacySchemaHash: false - Bind request/context metadata with
aadso ciphertext cannot be replayed across contexts. - Validate and cap payload sizes before accepting untrusted inputs.
Anti-patterns:
- Reusing shared hardcoded keys across environments.
- Logging plaintext payloads or raw key material.
- Disabling integrity checks or bypassing decrypt errors.
- Treating draft protocol versions as immutable long-term contracts.
- Branch protection enforcement guide:
docs/branch-protection-required-checks.md - External audit execution plan:
docs/independent-security-review.md - Runtime dependency minimality check:
npm run deps:check- Streaming encoder/decoder
- Optional GCM‑SIV mode
- WASM implementation for browser environments
- External security review and hardened protocol guidance
Run baseline local benchmarks:
npm run bench