Skip to content

Instantly share code, notes, and snippets.

@bradleybuda
Created August 3, 2013 23:02
Show Gist options
  • Save bradleybuda/6148311 to your computer and use it in GitHub Desktop.
Save bradleybuda/6148311 to your computer and use it in GitHub Desktop.
Token masking benchmark
require 'benchmark'
require 'securerandom'
require 'base64'
require 'bcrypt'
BCrypt::Engine.cost = 1
length = 32
token_from_session = SecureRandom.base64(length)
Benchmark.bmbm do |x|
x.report 'xor' do
master_csrf_token = Base64.strict_decode64(token_from_session)
one_time_pad = SecureRandom.random_bytes(length)
encrypted_csrf_token = one_time_pad.bytes.zip(master_csrf_token.bytes).map { |c1,c2| c1 ^ c2 }.pack('c*')
one_time_pad.concat(encrypted_csrf_token)
Base64.strict_encode64(one_time_pad)
end
x.report 'salt + sha1' do
master_csrf_token = Base64.strict_decode64(token_from_session)
salt = SecureRandom.random_bytes(length)
digest = Digest::SHA1.new
digest.update(salt)
digest.update(master_csrf_token)
salt.concat(digest.digest)
Base64.strict_encode64(salt)
end
x.report 'salt + md5' do
master_csrf_token = Base64.strict_decode64(token_from_session)
salt = SecureRandom.random_bytes(length)
digest = Digest::MD5.new
digest.update(salt)
digest.update(master_csrf_token)
salt.concat(digest.digest)
Base64.strict_encode64(salt)
end
x.report 'bcrypt' do
BCrypt::Password.create(token_from_session)
end
end
Rehearsal -----------------------------------------------
xor 0.000000 0.000000 0.000000 ( 0.000083)
salt + sha1 0.000000 0.000000 0.000000 ( 0.000314)
salt + md5 0.000000 0.000000 0.000000 ( 0.000250)
bcrypt 0.000000 0.000000 0.000000 ( 0.001144)
-------------------------------------- total: 0.000000sec
user system total real
xor 0.000000 0.000000 0.000000 ( 0.000090)
salt + sha1 0.000000 0.000000 0.000000 ( 0.000049)
salt + md5 0.000000 0.000000 0.000000 ( 0.000038)
bcrypt 0.000000 0.000000 0.000000 ( 0.001115)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment