Ruby BCrypt Example
BCrypt is an awesome way to hash passwords, it kicks the crap out of MD5, SHA, and so on.
This post will explain how to go about using BCrypt in Ruby.
First, install it, gem install bcrypt-ruby
, done? good, let’s continue.
require 'bcrypt'
password = BCrypt::Password.create('my-awesome-password')
if BCrypt::Password.new(password) == 'my-awesome-password'
puts 'It works!'
else
puts 'Fail'
end
That code includes the BCrypt library, hashes the my-awesome-password
string and then checks it with BCrypt::Password.new==
.
Test it by saving the code snippet into bcrypt-test.rb
and running ruby bcrypt-test.rb
.