Update features and dependencies
This commit is contained in:
+24
-3
@@ -1,10 +1,31 @@
|
||||
import { createCipheriv, createDecipheriv, randomBytes, createHash } from "crypto";
|
||||
import {
|
||||
createCipheriv,
|
||||
createDecipheriv,
|
||||
randomBytes,
|
||||
hkdfSync,
|
||||
} from "crypto";
|
||||
|
||||
const ALGORITHM = "aes-256-gcm";
|
||||
|
||||
// Static salt — committed to source so it is stable across deployments.
|
||||
// It does NOT need to be secret; its purpose is domain-separation and to
|
||||
// prevent the raw secret from being used directly as a key.
|
||||
const HKDF_SALT = Buffer.from("epicure-encryption-v1-salt", "utf8");
|
||||
const HKDF_INFO = Buffer.from("epicure-aes-256-gcm-key", "utf8");
|
||||
|
||||
function getKey(): Buffer {
|
||||
const secret = process.env["BETTER_AUTH_SECRET"] ?? "dev-secret-needs-32-bytes-padding!";
|
||||
return createHash("sha256").update(secret).digest();
|
||||
// Prefer a dedicated encryption secret; fall back to BETTER_AUTH_SECRET so
|
||||
// existing deployments keep working without an immediate migration.
|
||||
const secret =
|
||||
process.env["ENCRYPTION_SECRET"] ?? process.env["BETTER_AUTH_SECRET"];
|
||||
if (!secret) throw new Error("ENCRYPTION_SECRET (or BETTER_AUTH_SECRET) is required");
|
||||
|
||||
// HKDF-SHA256 provides proper key derivation with domain separation.
|
||||
// This replaces the previous raw SHA-256 hash which offered no stretching
|
||||
// or salt, making brute-force against leaked ciphertexts trivial.
|
||||
return Buffer.from(
|
||||
hkdfSync("sha256", secret, HKDF_SALT, HKDF_INFO, 32)
|
||||
);
|
||||
}
|
||||
|
||||
export function encrypt(plaintext: string): string {
|
||||
|
||||
Reference in New Issue
Block a user