crypto-check.js 961 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict'
  2. const crypto = require('crypto')
  3. const Benchmark = require('benchmark')
  4. const scmpCompare = require('../lib/scmpCompare')
  5. const compareFn = crypto.timingSafeEqual || scmpCompare
  6. // `safe-buffer` in case `Buffer.from` in newer versions of node aren't available
  7. const Buffer = require('safe-buffer').Buffer
  8. const HASH1 = Buffer.from('e727d1464ae12436e899a726da5b2f11d8381b26', 'hex')
  9. const HASH2 = Buffer.from('f727d1464ae12436e899a726da5b2f11d8381b26', 'hex')
  10. const suite = new Benchmark.Suite()
  11. suite.add('crypto check each fn call', function () {
  12. if (crypto.timingSafeEqual) {
  13. return crypto.timingSafeEqual(HASH1, HASH2)
  14. }
  15. return scmpCompare(HASH1, HASH2)
  16. })
  17. .add('crypto check once', function () {
  18. return compareFn(HASH1, HASH2)
  19. })
  20. .on('cycle', function (event) {
  21. console.log(String(event.target))
  22. })
  23. .on('complete', function () {
  24. console.log('Fastest is ' + this.filter('fastest').map('name'))
  25. })
  26. .run()