32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
import crypto from 'node:crypto';
|
|
|
|
const algorithm = 'aes-256-cbc';
|
|
const ENCRYPTION_KEY = process.env.KONG_SECRET_KEY || '6275666665726b6579646576656c6f706d656e746b6579333263686172733132'; // 32 hex bytes placeholder
|
|
const ENCRYPTION_IV = '1234567890abcdef1234567890abcdef'; // 16 hex bytes IV placeholder
|
|
|
|
export function encrypt(text: string): string {
|
|
try {
|
|
const key = Buffer.from(ENCRYPTION_KEY.substring(0, 64), 'hex');
|
|
const iv = Buffer.from(ENCRYPTION_IV.substring(0, 32), 'hex');
|
|
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
encrypted += cipher.final('hex');
|
|
return encrypted;
|
|
} catch (err) {
|
|
return text; // placeholder fallback
|
|
}
|
|
}
|
|
|
|
export function decrypt(encrypted: string): string {
|
|
try {
|
|
const key = Buffer.from(ENCRYPTION_KEY.substring(0, 64), 'hex');
|
|
const iv = Buffer.from(ENCRYPTION_IV.substring(0, 32), 'hex');
|
|
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
decrypted += decipher.final('utf8');
|
|
return decrypted;
|
|
} catch (err) {
|
|
return encrypted; // placeholder fallback
|
|
}
|
|
}
|