virtueller-stundenplan/classes/EncryptionHelper.js

34 lines
1.1 KiB
JavaScript

const crypto = require('node:crypto');
const { DateTime } = require('luxon');
class EncryptionHelper {
static encryptDataAESGCM(plainText, key) {
try {
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
const encryptedText = Buffer.concat([cipher.update(plainText, 'utf8'), cipher.final()]);
const authTag = cipher.getAuthTag();
return Buffer.concat([iv, encryptedText, authTag]);
} catch (error) {
console.error("Encryption failed:", error);
return null;
}
}
static makeEncryptedParameter(username, key) {
const timestamp = DateTime.now().setZone('Europe/Berlin');
const tsFormatted = timestamp.toFormat('yyyy-MM-dd HH:mm:ss');
const plainText = `${tsFormatted} ${username}`;
const hash = crypto.createHash('sha256').update(key, 'utf8').digest();
const encryptedData = this.encryptDataAESGCM(plainText, hash);
const base64Encoded = encryptedData.toString('base64');
return base64Encoded;
}
}
module.exports = EncryptionHelper