有时候我们会使用crypto模块对数据进行加密。比较经典的情形就是生产AccessToken.
今天就测试下Cipher使用AES256对于数据加密的效率。
生成accesstoken代码:
module.exports.generateAccessToken = function (content, callback) {
var cipher = crypto.createCipher('aes256', '##wusp^&*15578899366'); //password is ##wusp^&*15578899366
var encrypted = '';
cipher.on('readable', function () {
var data = cipher.read();
if(data){
encrypted += data.toString('hex');
}
});
cipher.on('end', function () { });
cipher.write(content);
cipher.end();
};
循环生产10W条AccessToken代码:
var tokenManager = require('./tokenManager');
var date = new Date();
var startTime = date.getTime();
for(var i = 1; i < 100000; i++){
tokenManager.generateAccessToken(JSON.stringify({name: 'wusp', age: '12', company: 'gxnews', uuid: 'd22f30b8-2716-41d2-84f2-4cd56bb75ecc', exp: ('2016-7-1' + i)}), function () { });
}
date = new Date();
var endTime = date.getTime();
console.log('json using time: ' + (endTime - startTime));
date = new Date();
startTime = date.getTime();
for(i = 1; i < 100000; i++) {
tokenManager.generateAccessToken('name=wusp, age=12, company=gxnews, uuid=d22f30b8-2716-41d2-84f2-4cd56bb75ecc, exp=2016-7-1' + i, function () { });
}
date = new Date();
endTime = date.getTime();
console.log('string using time: ' + (endTime - startTime));
测试结果:
1. json using time: 5234
2. string using time: 4435
网友评论