Class: Passlib::BCrypt
Overview
Handles bcrypt password hashing via the bcrypt gem.
Recognized hash formats: $2a$, $2b$, $2x$, $2y$ (all variants are accepted on load, new hashes are always produced in $2a$ format by the underlying gem).
Constant Summary
Constants included from Internal::DSL
Instance Attribute Summary
Attributes inherited from Password
Class Method Summary collapse
-
.create(secret, **options) ⇒ BCrypt
Creates a new bcrypt hash.
Instance Method Summary collapse
- #create(secret) ⇒ Object
-
#create_comparable(secret) ⇒ BCrypt
A new instance hashed with the same salt.
- #load(string) ⇒ Object
- #upgrade? ⇒ Boolean
Methods inherited from Password
available?, #initialize, #inspect, load, #pretty_print, #verify
Methods included from Internal::DSL
Constructor Details
This class inherits a constructor from Passlib::Password
Class Method Details
.create(secret, **options) ⇒ BCrypt
Creates a new bcrypt hash.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/passlib/bcrypt.rb', line 23 class BCrypt < Password external "bcrypt", "~> 3.0" register mcf: %w[2a 2b 2x 2y] :salt, :cost # @param secret [String] the plaintext password to re-hash # @return [BCrypt] a new instance hashed with the same salt def create_comparable(secret) = self.class.create(secret, salt: @salt) def upgrade? cost = @salt.split("$")[2].to_i cost != (config.cost || ::BCrypt::Engine::DEFAULT_COST) end def create(secret) @salt = config.salt || ::BCrypt::Engine.generate_salt(config.cost || ::BCrypt::Engine::DEFAULT_COST) ::BCrypt::Engine.hash_secret(secret, @salt) end def load(string) bcrypt = ::BCrypt::Password.new(string) @salt = bcrypt.salt bcrypt.to_str end end |
Instance Method Details
#create(secret) ⇒ Object
37 38 39 40 |
# File 'lib/passlib/bcrypt.rb', line 37 def create(secret) @salt = config.salt || ::BCrypt::Engine.generate_salt(config.cost || ::BCrypt::Engine::DEFAULT_COST) ::BCrypt::Engine.hash_secret(secret, @salt) end |
#create_comparable(secret) ⇒ BCrypt
Returns a new instance hashed with the same salt.
30 |
# File 'lib/passlib/bcrypt.rb', line 30 def create_comparable(secret) = self.class.create(secret, salt: @salt) |
#load(string) ⇒ Object
42 43 44 45 46 |
# File 'lib/passlib/bcrypt.rb', line 42 def load(string) bcrypt = ::BCrypt::Password.new(string) @salt = bcrypt.salt bcrypt.to_str end |
#upgrade? ⇒ Boolean
32 33 34 35 |
# File 'lib/passlib/bcrypt.rb', line 32 def upgrade? cost = @salt.split("$")[2].to_i cost != (config.cost || ::BCrypt::Engine::DEFAULT_COST) end |