virtueller-stundenplan/classes/EncryptionHelper.js
2025-03-22 17:46:00 +01:00

32 lines
1.0 KiB
JavaScript

const crypto = require('node:crypto');
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 = new Date().toISOString().replace('T', ' ').split('.')[0];
const plainText = `${timestamp} ${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