AES-256-GCM String Encryption

Encrypt and authenticate strings with AES-256-GCM, deriving the key from a passphrase and random salt via scrypt.

TypescriptSecurity Boundaries

Code

typescriptsnippet
42 lines
import { createCipheriv, createDecipheriv, randomBytes, scryptSync } from "node:crypto";

const ALGORITHM = "aes-256-gcm";

export interface EncryptedPayload {
  salt: string;
  iv: string;
  tag: string;
  data: string;
}

export function deriveAesKey(passphrase: string, salt: Buffer): Buffer {
  if (typeof passphrase !== "string" || passphrase.length < 12) {
    throw new RangeError("passphrase must be at least 12 characters");
  }
  return scryptSync(passphrase.normalize("NFKC"), salt, 32);
}

export function encryptString(plaintext: string, passphrase: string): EncryptedPayload {
  const salt = randomBytes(16);
  const iv = randomBytes(12);
  const cipher = createCipheriv(ALGORITHM, deriveAesKey(passphrase, salt), iv);
  const data = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
  return {
    salt: salt.toString("base64"),
    iv: iv.toString("base64"),
    tag: cipher.getAuthTag().toString("base64"),
    data: data.toString("base64"),
  };
}

export function decryptString(payload: EncryptedPayload, passphrase: string): string {
  const salt = Buffer.from(payload.salt, "base64");
  const iv = Buffer.from(payload.iv, "base64");
  const tag = Buffer.from(payload.tag, "base64");
  const decipher = createDecipheriv(ALGORITHM, deriveAesKey(passphrase, salt), iv);
  decipher.setAuthTag(tag);
  return Buffer.concat([
    decipher.update(Buffer.from(payload.data, "base64")),
    decipher.final(),
  ]).toString("utf8");
}

Inside the snippet

Count the functions deriveAesKey, encryptString and decryptString as the entire surface area of AES-256-GCM String Encryption.

node:crypto supplies createCipheriv, createDecipheriv, randomBytes, scryptSync — built-in surface, no package involved. The failure path is explicit — RangeError is raised when the guard condition holds.

It builds and returns a fresh value rather than reusing its inputs.

What to watch for

  • node:crypto is a Node.js built-in module — it ships with the runtime, so there is nothing to install.
  • A browser is the runtime here: crypto are not available in Node.
  • The guard typeof passphrase !== "string" || passphrase.length < 12 short-circuits the flow with an early throw when its condition holds.

Using it

The primary signature is export function deriveAesKey(passphrase: string, salt: Buffer)

Import deriveAesKey from the file and call it — the rest of the code is implementation detail.

Key statements

throw new RangeError("passphrase must be at least 12 characters");
return scryptSync(passphrase.normalize("NFKC"), salt, 32);
return Buffer.concat([
decipher.update(Buffer.from(payload.data, "base64")),
decipher.final(),
]).toString("utf8");

Interactive Tools for This Topic

FAQ

Does `deriveAesKey` modify its input?
It does not. Inputs are only read; the return value is freshly built.
How does the snippet fail?
Validation comes first — `RangeError` marks the failure at `typeof passphrase !== "string" || passphrase.length < 12`, before any partial work happens.
Do I need to install anything before running this?
Nothing needs installing: `node:crypto` ships with Node.js itself.