Lightweight AES-256-CTR helpers for Node.js with both programmatic and CLI interfaces.
npm install @ideadesignmedia/encryptionLightweight AES-256-CTR helpers for Node.js with both programmatic and CLI interfaces.
Source repository: https://github.com/ideadesignmedia/encryption
``bash`
npm install @ideadesignmedia/encryption
`js
const { encrypt, decrypt, randomString, KEY_LENGTH, IV_LENGTH } = require('@ideadesignmedia/encryption')
const key = randomString(KEY_LENGTH) // 32 ASCII characters; supply your own for production
const iv = '1234567891011121' // Must be 16 ASCII characters; update before shipping anything sensitive
const ciphertext = encrypt('hello world', key, iv)
console.log(ciphertext) // => hex encoded string
const plainText = decrypt(ciphertext, key, iv)
console.log(plainText) // => 'hello world'
`
Notes:
- Keys must be exactly KEY_LENGTH (32) ASCII characters (or a Buffer of 32 bytes) and initialization vectors must be IV_LENGTH (16) ASCII characters (Buffer of 16 bytes).encrypt
- Inputs to should be UTF-8 strings; decrypt expects a hex string produced by this package.randomString(length)
- returns a numeric string of the requested length; bring your own cryptographic key management for production secrets.
- Bundled TypeScript definitions export the helpers and constants shown above for ergonomic imports.
`ts
import { encrypt, decrypt, KEY_LENGTH, IV_LENGTH } from '@ideadesignmedia/encryption'
const key = '0123456789abcdef0123456789abcdef' // KEY_LENGTH = 32
const iv = '1234567891011121' // IV_LENGTH = 16
const cipher = encrypt('typed usage', key, iv)
const plain = decrypt(cipher, key, iv)
`
`bashEncrypt plain text (writes hex ciphertext to stdout)
npx @ideadesignmedia/encryption encrypt "super secret" 0123456789abcdef0123456789abcdef 1234567891011121
Run
npx @ideadesignmedia/encryption --help for inline usage information. Leaving the key and IV arguments empty uses the built-in defaults (intended for local testing only).Remember: CLI string inputs for
key and iv must be exactly 32 and 16 characters respectively (ASCII). Provide Buffers when working with non-ASCII data.API reference
| Function | Description |
| --- | --- |
|
encrypt(value, key?, iv?) | Encrypts a UTF-8 string using AES-256-CTR; returns a hex string. |
| decrypt(ciphertext, key?, iv?) | Decrypts a hex string generated by encrypt; returns a UTF-8 string. |
| randomString(length?) | Creates a numeric string of the supplied length (default 32). |
| KEY_LENGTH / IV_LENGTH | Exported constants so you can validate your own keys and IVs. |Development
`bash
npm install
npm testQuick sanity check
node -e "const {encrypt, decrypt} = require('./'); const out = encrypt('demo'); console.log(out, decrypt(out));"
``