Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr SMS Gateway8 * File : backend/src/security/crypto.ts9 * Module : security10 * Repository : https://git.spboucher.ai/maclustr-sms11 * Project Website : https://sms.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { createCipheriv, createDecipheriv, createHmac, randomBytes } from "node:crypto"43import { config } from "../config.ts"4445/** Chiffrement au repos : AES-256-GCM, format iv(12) | étiquette(16) | chiffré. Le contexte lie le chiffré à son usage. */46export function seal(plain: string, context: string): Buffer {47 const iv = randomBytes(12)48 const c = createCipheriv("aes-256-gcm", config.dataKey, iv)49 c.setAAD(Buffer.from(context))50 const ct = Buffer.concat([c.update(plain, "utf8"), c.final()])51 return Buffer.concat([iv, c.getAuthTag(), ct])52}53export function open(box: Buffer | Uint8Array | null | undefined, context: string): string | null {54 if (!box) return null55 const b = Buffer.from(box)56 const d = createDecipheriv("aes-256-gcm", config.dataKey, b.subarray(0, 12))57 d.setAAD(Buffer.from(context))58 d.setAuthTag(b.subarray(12, 28))59 return Buffer.concat([d.update(b.subarray(28)), d.final()]).toString("utf8")60}61export const hmacHex = (secret: string, data: string) => createHmac("sha256", secret).update(data).digest("hex")62