@haneinfo/credential-crypto
v0.1.0
Published
Pure Node AES-256-GCM encryption helpers for application-owned credentials.
Readme
@haneinfo/credential-crypto
Pure Node.js AES-256-GCM helpers for application-owned credentials. The package uses only
node:crypto; it does not read environment variables or depend on NestJS, Prisma, or settings.
Applications read their own configuration and pass canonical base64 keys to this package.
import {
createCredentialKeyRing,
decryptCredential,
encryptCredential,
parsePreviousCredentialKeys,
} from '@haneinfo/credential-crypto';
// Both calls throw CREDENTIAL_ENCRYPTION_UNAVAILABLE for a missing or invalid key or key list.
const ring = createCredentialKeyRing({
active: process.env.REPORTS_CONNECTION_ENCRYPTION_KEY ?? '',
previous: parsePreviousCredentialKeys(process.env.REPORTS_CONNECTION_PREVIOUS_KEYS),
});
const envelope = encryptCredential(ring, ['connection-id', 'password'], 'secret');
const secret = decryptCredential(ring, ['connection-id', 'password'], envelope);CommonJS applications can load the same API with require('@haneinfo/credential-crypto'). The
package is built as CommonJS and exposes TypeScript declarations for both module styles.
API
parseCredentialKey(raw)validates canonical base64 for exactly 32 bytes.parsePreviousCredentialKeys(json)parses an optional JSON array of at most ten key strings.credentialKeyId(key)returns the first 32 lowercase hex characters of SHA-256(key).createCredentialKeyRing({ active, previous? })validates the active and previous keys. Previous keys must be unique and different from the active key. The returned opaque ring keeps key bytes private and refuses JSON serialization.encryptCredential(ring, aad, plaintext)encrypts with the active key.aadmust be a nonempty string array and is encoded asJSON.stringify(aad).decryptCredential(ring, aad, envelope, options?)decrypts current envelopes. Legacy envelopes are considered only whenoptions.legacyAadis supplied; that byte sequence is used as their AAD.inspectCredential(envelope)checks the envelope structure without decrypting and returns its format and current key id, ornullfor malformed input.reencryptCredential(ring, aad, envelope, options?)re-encrypts legacy or previous-key data with the active key. It returns{ changed: false }for a structurally valid current envelope already using the active key.
Invalid/missing active keys throw CREDENTIAL_ENCRYPTION_UNAVAILABLE. Invalid envelopes, AAD,
unknown key ids, or authentication failures throw CREDENTIAL_DECRYPTION_FAILED; errors do not
include the supplied key, AAD, plaintext, or envelope.
Envelope formats
New ciphertext uses five colon-separated fields:
v1:<key-id>:<nonce-base64>:<tag-base64>:<ciphertext-base64>The nonce is 12 random bytes, the authentication tag is 16 bytes, and all base64 fields must be
canonical. The cipher is AES-256-GCM with a 16-byte authentication tag. Reports AAD remains
[connectionId, field].
The esign legacy format has four fields and no key id:
v1:<nonce-base64>:<tag-base64>:<ciphertext-base64>It is read only when the caller supplies the original credential id bytes as legacyAad. New
writes always use the five-field format.
Key rotation
- Generate a new 32-byte key and make it active. Keep the previous active key in
previous. - Deploy the application with both keys configured. Reads accept either key; writes use only the active key.
- Use
inspectCredentialto count key ids without decrypting, then callreencryptCredentialin the application's transactionally safe maintenance workflow for each stored value. - Confirm the old key count is zero, then remove it from the previous-key list.
The package does not provide persistence or a database migration script. Each application owns its transaction boundaries, maintenance controls, and secret backup procedure.
Settings boundary
This package does not use @haneinfo/settings. Settings are for runtime-adjustable nonsecret values;
credential keys and connection secrets stay in the application's secret configuration and storage.
