added bcrypt as one of the encryptors

This commit is contained in:
Julio Capote 2010-01-07 16:08:01 +08:00 committed by José Valim
parent 4d8f5ea165
commit 3916033058
3 changed files with 34 additions and 1 deletions

View File

@ -16,6 +16,7 @@ module Devise
autoload :RestfulAuthenticationSha1, 'devise/encryptors/restful_authentication_sha1'
autoload :Sha512, 'devise/encryptors/sha512'
autoload :Sha1, 'devise/encryptors/sha1'
autoload :BCrypt, 'devise/encryptors/bcrypt'
end
module Orm
@ -179,4 +180,4 @@ end
Warden::Strategies.clear!
Warden::Serializers.clear!
require 'devise/rails'
require 'devise/rails'

View File

@ -0,0 +1,24 @@
require "bcrypt"
module Devise
# Implements a way of adding different encryptions.
# The class should implement a self.digest method that taks the following params:
# - password
# - stretches: the number of times the encryption will be applied
# - salt: the password salt as defined by devise
# - pepper: Devise config option
#
module Encryptors
# = BCrypt
# Uses the BCrypt hash algorithm to encrypt passwords.
class BCrypt
# Gererates a default password digest based on stretches, salt, pepper and the
# incoming password. We don't strech it ourselves since BCrypt does so internally.
def self.digest(password, stretches, salt, pepper)
::BCrypt::Engine.hash_secret(password, [salt, pepper].flatten.join('xx'), stretches)
end
end
end
end

View File

@ -17,6 +17,14 @@ class Encryptors < ActiveSupport::TestCase
encryptor = Devise::Encryptors::ClearanceSha1.digest('123mudar', nil, '65c58472c207c829f28c68619d3e3aefed18ab3f', nil)
assert_equal clearance, encryptor
end
test 'should match a password created by bcrypt' do
bcrypt = "$2a$10$81UWRL4S01M6zxjMPyBame1He8EHYgdFm26rQh0qKzglf2ijtEyfa"
encryptor = Devise::Encryptors::BCrypt.digest('123mudar', 4, '$2a$10$81UWRL4S01M6zxjMPyBame', '')
assert_equal bcrypt, encryptor
end
Devise::ENCRYPTORS_LENGTH.each do |key, value|
test "should have length #{value} for #{key.inspect}" do