I've been testing the encryption/decryption performance of nodejs (well, crypto more specifically) to implement it in a project of mine. After a small amount of edits, I thought I had achieved a somewhat decent speed but then I talked to a friend and did some research, and now wish to know if there are any ways to do this more efficiently
I moved the require("crypto") to outside the function so it only runs once, tried saving the cipher and decipher in a variable (which didn't work), googling more efficient ways to encrypt/decript,etc but couldn't achieve much more performance
var crypt = require('crypto')
function encrypt(text,password){
var text1=String(text)
var cipher = crypt.createCipher('aes-128-cbc',password)
var crypted = cipher.update(text1,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text,password){
var text1=String(text)
var decipher = crypt.createDecipher('aes-128-cbc',password)
var dec = decipher.update(text1,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
function generatepass(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++){
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
text=text.toString()
return text;
}
var text=generatepass(50)
var pass=generatepass(50)
aa=performance.now()
for(var i=0;i<1000;i++){
decrypt(encrypt(text,pass),pass)
}
console.log((performance.now()-aa)/1000) //around 0.05ms on my end