/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
libs/application-generic/src/encryption/cipher.ts
28 строк
976 B
Adam Chmara
chore(root): apply biome formatter & linter fixes NV-6436 (#8838)
31 июл 2025, 14:59
Не верифицирован
31 июл 2025, 14:59
855fb8f
Код
Авторство
О чём код?
import { createCipheriv, createDecipheriv, randomBytes } from 'crypto'; const IV_LENGTH = 16; const CIPHER_ALGO = 'aes-256-cbc'; export function encrypt(text) { const ENCRYPTION_KEY = process.env.STORE_ENCRYPTION_KEY; const iv = randomBytes(IV_LENGTH); const cipher = createCipheriv(CIPHER_ALGO, Buffer.from(ENCRYPTION_KEY), iv); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); return `${iv.toString('hex')}:${encrypted.toString('hex')}`; } export function decrypt(text) { const ENCRYPTION_KEY = process.env.STORE_ENCRYPTION_KEY; const textParts = text.split(':'); const iv = Buffer.from(textParts.shift(), 'hex'); const encryptedText = Buffer.from(textParts.join(':'), 'hex'); const decipher = createDecipheriv(CIPHER_ALGO, Buffer.from(ENCRYPTION_KEY), iv); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); }