Skip to content

About

A sensitive log redaction engine for TypeScript.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

6 Commits

Folders and files

Repository files navigation

SecretEngine Banner

SecretEngine: Context-Bound Secret Utilities for TypeScript

TypeScript Crypto Cipher KDF License Changelog

SecretEngine helps TypeScript applications encrypt, mask, hash, generate, and verify sensitive values such as API keys, OAuth tokens, device codes, verification codes, and integration secrets. It uses AES-256-GCM for authenticated encryption and HKDF for purpose/context-bound key derivation.

It is designed for developers who need safer secret handling without scattering low-level crypto decisions across application code. Use it when encrypted values should only decrypt for the intended purpose and owner, when verification codes need constant-time checks, or when logs and UI need masked secret previews.

πŸ€– Ask your coding assistant

"Audit my codebase to see if adding the secret-engine package on npm is beneficial. If so, explain why and draft an integration plan identifying values to encrypt, purpose/context strings to use, hash-verification locations, and mask points using the package README and source code."


Why use SecretEngine?

Feature Without SecretEngine With SecretEngine
Encryption Details Apps repeat IV, auth tag, and key derivation code. πŸ”’ AES-256-GCM Envelopes. AES-256-GCM envelopes are handled consistently.
Context Binding A ciphertext may decrypt in the wrong feature or tenant context. πŸ”— HKDF Derivation. purpose and context are part of key derivation.
Code Verification String comparisons can leak timing information. ⚑ Timing-Safe Checking. verifySecretHash() uses constant-time comparison.
Secret Display UI previews often duplicate masking logic. πŸ‘οΈ UI Masking. maskSecret() provides one masking primitive.
Code Generation Verification codes need consistent length/alphabet/chunking. 🎲 Secure Generation. generateCode() handles secure random code generation.

Installation

Install SecretEngine via your preferred package manager:

# npm
npm install secret-engine

# pnpm
pnpm add secret-engine

# bun
bun add secret-engine

# yarn
yarn add secret-engine

Quick Start

import { SecretEngine } from "secret-engine";

const secrets = new SecretEngine({
  masterKey: process.env.SECRET_ENCRYPTION_KEY!,
});

const encrypted = await secrets.encrypt("sk_live_abc", {
  purpose: "provider-api-key",
  context: `user:${userId}`,
});

const plain = await secrets.decrypt(encrypted, {
  purpose: "provider-api-key",
  context: `user:${userId}`,
});

Changing either purpose or context causes decryption to fail.

Use stable, non-empty purpose and context strings. A good pattern is purpose for the feature boundary ("provider-api-key", "device-code") and context for the owner or tenant boundary ("user:123", "tenant:acme"). Reusing the same pair intentionally reuses the derived key.


Practical Examples

Encrypt user-owned API keys

const encryptedKey = await secrets.encrypt(apiKey, {
  purpose: "api-key",
  context: `user:${user.id}`,
});

Generate and verify codes

import { generateCode, hashSecret, verifySecretHash } from "secret-engine";

const code = generateCode({ length: 6, chunkSize: 3 });
const storedHash = hashSecret(code, {
  secret: process.env.CODE_HASH_SECRET!,
  normalize: (value) => value.replace(/-/g, "").toUpperCase(),
});

const valid = verifySecretHash(input, storedHash, {
  secret: process.env.CODE_HASH_SECRET!,
  normalize: (value) => value.replace(/-/g, "").toUpperCase(),
});

Mask secrets for UI or logs

import { maskSecret } from "secret-engine";

maskSecret("sk_live_abcdef123456");
// "sk_lβ€’β€’β€’β€’β€’β€’3456"

Use low-level helpers

import { decryptWithKey, deriveKeyHKDF, encryptWithKey } from "secret-engine";

const key = await deriveKeyHKDF(masterKey, "integration-secret", "tenant:acme");

const envelope = encryptWithKey("secret value", key);
const plain = decryptWithKey(envelope, key);

API Reference

Export Purpose
SecretEngine High-level encrypt/decrypt class using a caller-provided master key.
encryptWithKey(value, key) Encrypts a string with AES-256-GCM.
decryptWithKey(envelope, key) Decrypts an AES-256-GCM envelope.
deriveKeyHKDF(masterKey, salt, info) Derives a 32-byte key for a purpose/context.
maskSecret(value, options?) Masks a secret while keeping optional prefix/suffix characters.
generateCode(options?) Generates secure random codes with configurable alphabet and chunking.
hashSecret(value, options) Creates an HMAC hash for later verification.
verifySecretHash(value, hash, options) Verifies a hash using constant-time comparison.

Development

To build the package and generate TypeScript declarations:

bun run build

To run the package unit tests:

bun run test

To run the package type check:

bun run typecheck

After building, verify the published runtime exports:

bun run test:smoke

Related Packages


License

MIT Β© Christian Paul

About

A sensitive log redaction engine for TypeScript.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages