mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
added bcrypt as one of the encryptors
This commit is contained in:
parent
4d8f5ea165
commit
3916033058
3 changed files with 34 additions and 1 deletions
|
@ -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
|
||||
|
|
24
lib/devise/encryptors/bcrypt.rb
Normal file
24
lib/devise/encryptors/bcrypt.rb
Normal 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
|
|
@ -18,6 +18,14 @@ class Encryptors < ActiveSupport::TestCase
|
|||
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
|
||||
swap Devise, :encryptor => key do
|
||||
|
|
Loading…
Reference in a new issue