23 lines
886 B
JavaScript
Executable File
23 lines
886 B
JavaScript
Executable File
import crypto from 'crypto';
|
||
|
||
const algorithm = 'aes-256-ecb'; // Verwende ECB-Modus, der keinen IV benötigt
|
||
const secret = process.env.SECRET_KEY;
|
||
if (!secret) {
|
||
console.warn('[crypto] SECRET_KEY fehlt – verwende unsicheren Fallback (nur Entwicklung).');
|
||
}
|
||
const key = crypto.scryptSync(secret || 'DEV_FALLBACK_SECRET', 'salt', 32); // Der Schlüssel sollte eine Länge von 32 Bytes haben
|
||
|
||
export const encrypt = (text) => {
|
||
const cipher = crypto.createCipheriv(algorithm, key, null); // Kein IV verwendet
|
||
let encrypted = cipher.update(text, 'utf8', 'hex');
|
||
encrypted += cipher.final('hex');
|
||
return encrypted;
|
||
};
|
||
|
||
export const decrypt = (text) => {
|
||
const decipher = crypto.createDecipheriv(algorithm, key, null); // Kein IV verwendet
|
||
let decrypted = decipher.update(text, 'hex', 'utf8');
|
||
decrypted += decipher.final('utf8');
|
||
return decrypted;
|
||
};
|