Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does has_secure_password use any form of salting?

I want to use has_secure_password to store encrypted passwords in the database. I can't find on the the internet if has_secure_password uses any form of salting. If it uses salting, how does it works? Can anyone clarify this for me?

Thijs

like image 346
Thijs Avatar asked Apr 13 '12 11:04

Thijs


People also ask

How does bcrypt work in Rails?

bcrypt provides a password-hashing algorithm that allows us to add secure authentication to our Rails sites. A hash algorithm takes data (in this case, a password) and hashes it using an algorithm. A password hash combines a user's password with a piece of random data known as salt.

What is bcrypt Ruby?

bcrypt() is a sophisticated and secure hash algorithm designed by The OpenBSD project for hashing passwords. The bcrypt Ruby gem provides a simple wrapper for safely handling passwords.


1 Answers

has_secure_password uses bcrypt-ruby. bcrypt-ruby automatically handles the storage and generation of salts for you. A typical hash from bcrypt-ruby looks like this: $2a$10$4wXszTTd7ass8j5ZLpK/7.ywXXgDh7XPNmzfIWeZC1dMGpFghd92e. This hash is split internally using the following function:

def split_hash(h)   _, v, c, mash = h.split('$')   return v, c.to_i, h[0, 29].to_str, mash[-31, 31].to_str end 

For the example hash this function yields:

  • version: 2a
  • cost: 10
  • salt: $2a$10$4wXszTTd7ass8j5ZLpK/7.
  • hash: ywXXgDh7XPNmzfIWeZC1dMGpFghd92e

The ==-function of BCrypt::Password extracts the salt and applies it to the passed string:

BCrypt::Password.create('bla') == 'bla' # => true 
like image 51
fabi Avatar answered Sep 27 '22 15:09

fabi