Aggiornamento cataloghi...

This commit is contained in:
Surya Paolo
2025-04-11 18:49:59 +02:00
parent e1ca4ef17f
commit 8e8a3204a7
13 changed files with 509 additions and 88 deletions

View File

@@ -56,8 +56,8 @@ const readline = require('readline');
// Code goes here
const keySize = 256;
const ivSize = 128;
const iterations = 100;
// const ivSize = 128;
const iterations = 1000;
if (!!process.env.GCM_API_KEY && process.env.GCM_API_KEY !== '') {
webpush.setGCMAPIKey(process.env.GCM_API_KEY);
@@ -585,6 +585,7 @@ module.exports = {
RISO: '13',
FIOREDELLAVITA: '15',
PIUCHEBUONO: '17',
MACRO: '18',
IDAPP_BOTONGROUP: '1000',
@@ -3904,47 +3905,79 @@ module.exports = {
},
encrypt(msg, pass) {
var salt = CryptoJS.lib.WordArray.random(128 / 8);
try {
// Genera salt e IV casuali
const salt = CryptoJS.lib.WordArray.random(128 / 8); // 16 byte
const iv = CryptoJS.lib.WordArray.random(128 / 8); // 16 byte
var key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations: iterations,
});
// Deriva la chiave usando PBKDF2
const key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations: iterations,
});
var iv = CryptoJS.lib.WordArray.random(128 / 8);
// Critta il messaggio
const encrypted = CryptoJS.AES.encrypt(msg, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
});
var encrypted = CryptoJS.AES.encrypt(msg, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
});
// salt, iv will be hex 32 in length
// append them to the ciphertext for use in decryption
var transitmessage = salt.toString() + iv.toString() + encrypted.toString();
return transitmessage;
// Concatena salt, IV e ciphertext con un delimitatore
const transitmessage = `${salt.toString()}|${iv.toString()}|${encrypted.toString()}`;
return transitmessage;
} catch (error) {
console.error('❌❌❌ Errore durante la crittazione:', error.message);
throw error;
}
},
decrypt(transitmessage, pass) {
var salt = CryptoJS.enc.Hex.parse(transitmessage.substr(0, 32));
var iv = CryptoJS.enc.Hex.parse(transitmessage.substr(32, 32));
var encrypted = transitmessage.substring(64);
try {
let saltHex, ivHex, encrypted;
var key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations: iterations,
});
// Controlla se il messaggio è nel nuovo formato (con delimitatore)
if (transitmessage.includes('|')) {
const parts = transitmessage.split('|');
if (parts.length !== 3) {
throw new Error('❌❌❌ Formato del messaggio non valido.');
}
[saltHex, ivHex, encrypted] = parts;
} else {
// Vecchio formato: salt (32 caratteri), IV (32 caratteri), ciphertext (resto)
saltHex = transitmessage.substr(0, 32);
ivHex = transitmessage.substr(32, 32);
encrypted = transitmessage.substring(64);
}
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
// Converte salt e IV da esadecimale
const salt = CryptoJS.enc.Hex.parse(saltHex);
const iv = CryptoJS.enc.Hex.parse(ivHex);
});
return decrypted;
// Deriva la chiave usando PBKDF2
const key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations: iterations,
});
// Decritta il messaggio
const decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
});
// Restituisci il messaggio originale in formato UTF-8
let ris = decrypted.toString(CryptoJS.enc.Utf8);
// console.log('RIS', ris);
return ris;
} catch (error) {
console.error('❌❌❌ Errore durante la decrittazione:', error.message);
throw error;
}
},
cryptdata(mydata) {
if (mydata === '')
return '';
@@ -3954,13 +3987,17 @@ module.exports = {
},
decryptdata(mydatacrypted) {
if (mydatacrypted === '' || mydatacrypted === undefined)
if (!mydatacrypted) return '';
try {
// console.log('SECRK', process.env.SECRK);
const decr = this.decrypt(mydatacrypted, process.env.SECRK);
// console.log('decr:', mydatacrypted, '->', decr);
return decr;
} catch (error) {
console.error('❌❌❌❌❌ Decryption error:', error);
return '';
// Decrypt
// const bytes = CryptoJS.AES.decrypt(mydatacrypted.toString(), process.env.SECRK);
// return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
return this.decrypt(mydatacrypted, process.env.SECRK).
toString(CryptoJS.enc.Utf8);
}
},
BoolToInt(mybool) {
@@ -6025,6 +6062,44 @@ module.exports = {
// Funzione per implementare il ritardo tra i tentativi
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
/**
* Converte una data in formato italiano GG/MM/YYYY in un oggetto Date e restituisce il timestamp.
* @param {string} dateString - La data in formato GG/MM/YYYY.
* @returns {Object} - Un oggetto contenente la data (Date) e il timestamp (number).
* Restituisce null se la data non è valida.
*/
convertiDataItaliana(dateString) {
// Verifica che la data sia una stringa valida
if (!dateString || typeof dateString !== 'string') {
console.error("Input non valido: la data deve essere una stringa.");
return null;
}
// Rimuovi eventuali spazi bianchi e dividi la data in GG, MM, YYYY
const [giorno, mese, anno] = dateString.trim().split('/').map(Number);
// Controlla che i valori siano stati estratti correttamente
if (isNaN(giorno) || isNaN(mese) || isNaN(anno)) {
console.error("Formato data non valido:", dateString);
return null;
}
// Crea un oggetto Date (mese parte da 0 in JavaScript)
const dateObj = new Date(anno, mese - 1, giorno);
// Verifica che la data sia valida
if (isNaN(dateObj.getTime())) {
console.error("Data non valida:", dateString);
return null;
}
// Restituisci l'oggetto con la data e il timestamp
return {
date: dateObj,
timestamp: dateObj.getTime()
};
}