Skip to content

Instantly share code, notes, and snippets.

@lkfken
Created June 11, 2016 00:41
Show Gist options
  • Save lkfken/4dacefa09b60341219db377af5d5586d to your computer and use it in GitHub Desktop.
Save lkfken/4dacefa09b60341219db377af5d5586d to your computer and use it in GitHub Desktop.
Ruby BCrypt example
require 'bcrypt'
require 'pp'
secret = 'my password'
BCrypt::Engine.cost = 4 # default is 10
password = BCrypt::Password.create(secret)
puts '********** create **********'
puts ['version:', password.version].join(' ')
puts ['cost:', password.cost].join(' ')
puts ['salt:', password.salt].join(' ')
puts ['checksum:', password.checksum].join(' ')
# this is actually comparing the password hash, and not the password string itself.
# alias_method: BCrypt::Password#is_password?(secret)
pp password == secret
puts '********** read **********'
# with the password and the salt, this .hash_secret return the password hash
# same as BCrypt::Password#to_s
hash_secret = BCrypt::Engine.hash_secret(secret, password.salt)
puts ['Hash Secret:', hash_secret].join(' ')
password = BCrypt::Password.new(hash_secret)
pp password == secret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment