FearlessVPNNG/encryption.js

45 lines
1.3 KiB
JavaScript

const crypto = require('crypto');
function encrypt(payload, key, secret_iv, algorithm) {
try {
const iv = crypto
.createHash('sha512')
.update(secret_iv)
.digest('hex')
.substring(0, 16)
const cipher = crypto.createCipheriv(algorithm, crypto
.createHash('sha512')
.update(key)
.digest('hex')
.substring(0, 32), iv);
let encrypted = cipher.update(payload, 'binary', 'binary');
encrypted += cipher.final('binary');
return Buffer.from(encrypted, 'binary');
} catch (error) {
return Buffer.from('');
}
}
function decrypt(payload, key, secret_iv, algorithm) {
try {
const iv = crypto
.createHash('sha512')
.update(secret_iv)
.digest('hex')
.substring(0, 16)
const decipher = crypto.createDecipheriv(algorithm, crypto
.createHash('sha512')
.update(key)
.digest('hex')
.substring(0, 32), iv);
let decrypted = decipher.update(payload, 'binary', 'binary');
decrypted += decipher.final('binary');
return Buffer.from(decrypted, 'binary');
} catch (error) {
return Buffer.from('');
}
}
module.exports = {
encrypt, decrypt
};