import Password from "../tools/Password"; import crypto from "crypto"; import { secureValuesPublicKey } from "../../constants"; export const generateSecureValue = (content: string) => { // Generate a hash of the content const hash = Password.hash(content); // Parse the PEM key into a proper KeyObject const publicKey = crypto.createPublicKey(secureValuesPublicKey); // Encrypt the content using the .secureValues.pub public key const encrypted = crypto.publicEncrypt( { key: publicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: "sha256", }, Buffer.from(content, "utf-8"), ); // Return the encrypted content and the hash for storage return { encrypted: encrypted.toString("base64"), hash, }; };