@openade/fe
Version:
Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)
41 lines • 1.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncryptionService = void 0;
const crypto_1 = require("crypto");
const util_1 = require("util");
const scryptAsync = (0, util_1.promisify)(crypto_1.scrypt);
class EncryptionService {
async encrypt(data, key) {
const algorithm = 'aes-256-cbc';
const keyBuffer = await this.deriveKey(key);
const iv = (0, crypto_1.randomBytes)(16);
const cipher = (0, crypto_1.createCipheriv)(algorithm, keyBuffer, iv);
let encrypted = cipher.update(data);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return Buffer.concat([iv, encrypted]);
}
async decrypt(encryptedData, key) {
const algorithm = 'aes-256-cbc';
const keyBuffer = await this.deriveKey(key);
const iv = encryptedData.slice(0, 16);
const encrypted = encryptedData.slice(16);
const decipher = (0, crypto_1.createDecipheriv)(algorithm, keyBuffer, iv);
let decrypted = decipher.update(encrypted);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted;
}
async generateKey() {
return (0, crypto_1.randomBytes)(32).toString('hex');
}
async hash(data, algorithm = 'sha256') {
return (0, crypto_1.createHash)(algorithm).update(data).digest('hex');
}
async hmac(data, key, algorithm = 'sha256') {
return (0, crypto_1.createHmac)(algorithm, key).update(data).digest('hex');
}
async deriveKey(password) {
return scryptAsync(password, 'salt', 32);
}
}
exports.EncryptionService = EncryptionService;
//# sourceMappingURL=encryption.service.js.map