23 lines
764 B
JavaScript
23 lines
764 B
JavaScript
const crypto = require('crypto');
|
|
const path = require('path');
|
|
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
|
|
|
const ALGORITHM = 'aes-256-cbc';
|
|
const SECRET_KEY = crypto.createHash('sha256').update(process.env.CRYPTO_KEY || 'default_secret_key_12345').digest();
|
|
const IV_LENGTH = 16;
|
|
|
|
const text = process.argv[2];
|
|
if (!text) {
|
|
console.log('Utilizzo: node encrypt.js "testo da criptare"');
|
|
process.exit(1);
|
|
}
|
|
|
|
const iv = crypto.randomBytes(IV_LENGTH);
|
|
const cipher = crypto.createCipheriv(ALGORITHM, SECRET_KEY, iv);
|
|
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
encrypted += cipher.final('hex');
|
|
const result = iv.toString('hex') + ':' + encrypted;
|
|
|
|
console.log('Testo Originale:', text);
|
|
console.log('Testo Criptato: ', result);
|