Initial commit
This commit is contained in:
32
classes/EncryptionHelper.js
Normal file
32
classes/EncryptionHelper.js
Normal file
@@ -0,0 +1,32 @@
|
||||
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
|
||||
Reference in New Issue
Block a user