Escribí el código sobre encriptar la cadena que pasa en la función cryto con el archivo .env pero aparece la línea de error, no sé qué significa eso. Configuro los valores en el archivo .env con el entorno y los exporto por defecto y los uso en caso de que los necesite, pero la línea de error muestra "Ninguna sobrecarga coincide con esta llamada. La última sobrecarga dio el siguiente error.
Argument of type 'string | undefined' is not assignable to parameter of type 'WithImplicitCoercion<string> | { [Symbol.toPrimitive](hint: "string"): string; }'. Type 'undefined' is not assignable to type 'WithImplicitCoercion<string> | { [Symbol.toPrimitive](hint: "string"): string; }"como esto.
import { randomBytes, createCipheriv, createDecipheriv } from "crypto"; import environment from "../environment"; const ENCRYPTION_KEY = environment.encrypt_Key; // const enc = "klyH2NOdPmmFPtdCAHIIGgjMowUUd69P"; const IV_LENGTH = 16; export const encrypt = async (text: string) => { try { let iv = randomBytes(IV_LENGTH); let cipher = createCipheriv("aes-256-cbc", Buffer.from(ENCRYPTION_KEY), iv); let encrypted = Buffer.concat([cipher.update(text), cipher.final()]); return `${iv.toString("hex")}:${encrypted.toString("hex")}`; } catch (e) { console.error(e); } };Lo más probable es que su environment esté definido de tal manera que devuelva una string o undefined para una clave. Una forma de manejarlo es verificar eso y arrojar un error como este:
export const encrypt = async (text: string) => { // check if ENCRYPTION_KEY is set if(!ENCRYPTION_KEY) { throw new Error("encrypt_Key is not set"); } try { let iv = randomBytes(IV_LENGTH); let cipher = createCipheriv("aes-256-cbc", Buffer.from(ENCRYPTION_KEY), iv); let encrypted = Buffer.concat([cipher.update(text), cipher.final()]); return `${iv.toString("hex")}:${encrypted.toString("hex")}`; } catch (e) { console.error(e); } };