Skip to content

Instantly share code, notes, and snippets.

@cbiffle
Created August 8, 2012 17:48
Show Gist options
  • Save cbiffle/3297028 to your computer and use it in GitHub Desktop.
Save cbiffle/3297028 to your computer and use it in GitHub Desktop.
Multiple round hash benchmark (CoffeeScript/node.js)
#!/usr/bin/env coffee
#
# This simulates a common pattern for deriving encryption keys
# from passwords. It's somewhat alarming how fast computers
# have become. You may have to try round counts over 100k to
# get a useful measurement!
#
# To use:
# coffee hash-benchmark.coffee <algo> <rounds>
#
# Algo name options are in the node.js crypto docs, try sha256.
crypto = require 'crypto'
[ coffee, prog, algo, rounds ] = process.argv
data = new Buffer("seed!")
before = new Date
for i in [rounds..1]
hash = crypto.createHash(algo)
hash.update(data)
data = hash.digest()
after = new Date
console.log "Took #{after - before} milliseconds"
console.log "Final content: #{new Buffer(data).toString('hex')}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment