Skip to content

Instantly share code, notes, and snippets.

@maxktz
maxktz / gist:1be6d7dcc09560266ea44aba14d5d1a8
Created September 12, 2024 08:57
JS/TS encrypt string with password using AES and decrypt methods
import crypto from "crypto";
/** Helper function to generate a random salt */
export function generateSalt(length: number = 32): string {
return crypto.randomBytes(length).toString("hex");
}
/** Derive a key from a password and salt */
export function deriveKey(password: string, salt: string): Buffer {
return crypto.pbkdf2Sync(password, salt, 100000, 32, "sha256");