Skip to content

Instantly share code, notes, and snippets.

@gkwang
Created October 9, 2019 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkwang/904302e1d8d72c2d52fce8bd8a27cd6a to your computer and use it in GitHub Desktop.
Save gkwang/904302e1d8d72c2d52fce8bd8a27cd6a to your computer and use it in GitHub Desktop.
var lib = process.argv[2] || 'node'
var alg = 'sha512'
var length = 32
var libs = {
node: function (alg) {
var crypto = require('crypto')
return function (key, salt, iterations, length) {
return crypto.pbkdf2Sync(key, salt, iterations, length, 'sha512')
}
},
md5: function (alg) {
var crypto = require('crypto')
return function (key, salt, iterations, length) {
return crypto.createHash('md5').update(key).digest('hex');
}
},
}
console.log('iterations (N), time (ms), iterations/ms (ops/ms), hash')
var M = 10 * 1000
var prev = 0
var pbk = libs[lib](alg)
;(function loop (i) {
if (i > 80) return
var n = Math.pow(M, i / 80) | 0
if (n === prev) return loop(i + 1)
prev = n
var start = Date.now()
var end
var j = 0
do {
var _hash = pbk('whatever', 'salty', n, length)
end = Date.now()
j++
} while (end - start < 100)
var time = end - start
// if the first argument is a string,
// console.log doesn't quote things
//console.log(['' + n, time / j, (n * j) / time, _hash].join(', '))
console.log(['' + n, time / j, (n * j) / time].join(', '))
setTimeout(loop, 10, i + 1)
})(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment