Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const crypto = require('crypto');
- const originBuffer = crypto.randomBytes(1024*1024*8), //8MB
- encriptKey = crypto.randomBytes(8).toString('base64');
- /*
- Hashes
- */
- let ret = [];
- crypto.getHashes().forEach((hashName, i, array)=>{
- const hashObj = crypto.createHash(hashName);
- const startTime = process.hrtime();
- hashObj.update(originBuffer);
- const retLength = hashObj.digest('hex').length;
- const diffTime = process.hrtime(startTime),
- usedTime = diffTime[0]*1e3 + diffTime[1]/1e6; //ms
- ret.push({
- name: hashName,
- usedTime,
- retLength
- });
- console.log(`Hash done ${i+1}/${array.length}.\t`+hashName);
- });
- ret.sort((a, b)=>{
- return a.usedTime - b.usedTime;
- });
- console.log('\nHash Test Result:\n');
- console.log('time ↓\tretLength\tname');
- console.log('=====================================');
- for(let r of ret)
- console.log(`${r.usedTime.toFixed(2)}\t${r.retLength}\t${r.name}`);
- /**
- * ciphers
- */
- ret = [];
- crypto.getCiphers().forEach((cryptoName, i, array)=>{
- //these method will be break
- const skipNames = ['id-aes128-wrap','id-aes192-wrap','id-aes256-wrap','rc2','rc2-cfb','rc2-cbc','rc2-ofb','rc2-40-cbc','rc4-40','idea','idea-ofb','idea-cfb','idea-ecb','seed-ecb','id-smime-alg-CMS3DESwrap'];
- if(skipNames.indexOf(cryptoName)>=0)
- return;
- //cipher
- const cipher = crypto.createCipher(cryptoName, encriptKey);
- let startTime = process.hrtime();
- let cipherRet = cipher.update(originBuffer, 'binary', 'hex');
- cipherRet += cipher.final('hex');
- let diffTime = process.hrtime(startTime);
- let cipherTime = diffTime[0]*1e3 + diffTime[1]/1e6; //ms
- //decipher
- let decipherTime, avgTime;
- try {
- const decipher = crypto.createDecipher(cryptoName, encriptKey);
- startTime = process.hrtime();
- decipher.update(cipherRet, 'hex', 'binary');
- decipher.final('binary');
- diffTime = process.hrtime(startTime);
- decipherTime = diffTime[0]*1e3 + diffTime[1]/1e6; //ms
- avgTime = cipherTime/2 + decipherTime/2;
- decipherTime = decipherTime.toFixed(2);
- cipherTime = cipherTime.toFixed(2);
- }
- catch(e) {
- //disallow decipher
- decipherTime = 'unknown';
- avgTime = cipherTime;
- cipherTime = cipherTime.toFixed(2);
- }
- ret.push({
- cryptoName,
- cipherTime,
- decipherTime,
- avgTime
- });
- console.log(`Cipher done ${i+1}/${array.length}.\t`+cryptoName);
- });
- ret.sort((a, b)=>{
- return a.avgTime - b.avgTime;
- });
- console.log('\nCipher Test Result:\n');
- console.log('cipherTime\tdecipherTime\tavgTime ↓\tname');
- console.log('=====================================');
- for(let r of ret)
- console.log(`${r.cipherTime}\t${r.decipherTime}\t${r.avgTime.toFixed(2)}\t${r.cryptoName}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement