Advertisement
Guest User

Node crypto test

a guest
Mar 20th, 2017
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const crypto = require('crypto');
  2.  
  3. const originBuffer = crypto.randomBytes(1024*1024*8), //8MB
  4.       encriptKey = crypto.randomBytes(8).toString('base64');
  5.  
  6.  
  7. /*
  8.     Hashes
  9. */
  10. let ret = [];
  11. crypto.getHashes().forEach((hashName, i, array)=>{
  12.     const hashObj = crypto.createHash(hashName);
  13.     const startTime = process.hrtime();
  14.    
  15.     hashObj.update(originBuffer);
  16.     const retLength = hashObj.digest('hex').length;
  17.    
  18.     const diffTime = process.hrtime(startTime),
  19.           usedTime = diffTime[0]*1e3 + diffTime[1]/1e6; //ms
  20.    
  21.     ret.push({
  22.        name: hashName,
  23.        usedTime,
  24.        retLength
  25.     });
  26.    
  27.     console.log(`Hash done ${i+1}/${array.length}.\t`+hashName);
  28. });
  29.  
  30. ret.sort((a, b)=>{
  31.     return a.usedTime - b.usedTime;
  32. });
  33.  
  34. console.log('\nHash Test Result:\n');
  35. console.log('time ↓\tretLength\tname');
  36. console.log('=====================================');
  37. for(let r of ret)
  38.     console.log(`${r.usedTime.toFixed(2)}\t${r.retLength}\t${r.name}`);
  39.  
  40.  
  41. /**
  42.  * ciphers
  43.  */
  44. ret = [];
  45. crypto.getCiphers().forEach((cryptoName, i, array)=>{  
  46.     //these method will be break
  47.     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'];
  48.     if(skipNames.indexOf(cryptoName)>=0)
  49.         return;
  50.    
  51.     //cipher
  52.     const cipher  = crypto.createCipher(cryptoName, encriptKey);
  53.     let startTime = process.hrtime();
  54.    
  55.     let cipherRet = cipher.update(originBuffer, 'binary', 'hex');
  56.     cipherRet += cipher.final('hex');
  57.    
  58.     let diffTime = process.hrtime(startTime);
  59.     let cipherTime = diffTime[0]*1e3 + diffTime[1]/1e6; //ms
  60.    
  61.     //decipher
  62.     let decipherTime, avgTime;
  63.     try {
  64.         const decipher = crypto.createDecipher(cryptoName, encriptKey);
  65.         startTime = process.hrtime();
  66.        
  67.         decipher.update(cipherRet, 'hex', 'binary');
  68.         decipher.final('binary');
  69.        
  70.         diffTime = process.hrtime(startTime);
  71.         decipherTime = diffTime[0]*1e3 + diffTime[1]/1e6; //ms
  72.         avgTime = cipherTime/2 + decipherTime/2;
  73.        
  74.         decipherTime = decipherTime.toFixed(2);
  75.         cipherTime = cipherTime.toFixed(2);
  76.     }
  77.     catch(e) {
  78.         //disallow decipher
  79.         decipherTime = 'unknown';
  80.         avgTime = cipherTime;
  81.        
  82.         cipherTime = cipherTime.toFixed(2);
  83.     }
  84.    
  85.    
  86.     ret.push({
  87.        cryptoName,
  88.        cipherTime,
  89.        decipherTime,
  90.        avgTime
  91.     });
  92.    
  93.     console.log(`Cipher done ${i+1}/${array.length}.\t`+cryptoName);
  94. });
  95.  
  96. ret.sort((a, b)=>{
  97.     return a.avgTime - b.avgTime;
  98. });
  99.  
  100. console.log('\nCipher Test Result:\n');
  101. console.log('cipherTime\tdecipherTime\tavgTime ↓\tname');
  102. console.log('=====================================');
  103. for(let r of ret)
  104.     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