@viyuni/cookie-crypto
v0.0.1
Published
Standalone cookie encryption/decryption utilities with dynamic IV support.
Maintainers
Readme
@viyuni/cookie-crypto
Standalone cookie encryption/decryption utility package.
Features
- No API dependency
- AES-256-GCM authenticated encryption
- Dynamic IV by default (random IV per encryption)
- Optional custom IV / IV length
- Optional AAD support
Build
pnpm --filter @viyuni/cookie-crypto install
pnpm --filter @viyuni/cookie-crypto run buildtsdown outputs to dist/:
dist/index.js(ESM)dist/index.cjs(CJS)dist/index.d.ts(types)
Usage
import { encryptCookie, decryptCookie, createCookieCrypto } from '@viyuni/cookie-crypto';
const secret = 'your-strong-secret';
const cookie = 'session=abc123; Path=/; HttpOnly';
const encrypted = encryptCookie(cookie, secret);
const decrypted = decryptCookie(encrypted, secret);
const cryptoBox = createCookieCrypto(secret);
const encrypted2 = cryptoBox.encrypt(cookie, { ivLength: 16 });
const decrypted2 = cryptoBox.decrypt(encrypted2);Payload format
v1.<iv_base64url>.<ciphertext_base64url>.<tag_base64url>
Each encryption generates a new random IV by default, so repeated encryption of the same cookie yields different ciphertext.
