Class: HTAuth::Bcrypt

Inherits:
Algorithm show all
Defined in:
lib/htauth/bcrypt.rb

Overview

Internal: an implementation of the Bcrypt based encoding algorithm as used in the apache htpasswd -B option

Constant Summary collapse

DEFAULT_APACHE_COST =

this is the default cost from htpasswd

5

Constants inherited from Algorithm

Algorithm::ARGON2, Algorithm::BCRYPT, Algorithm::CRYPT, Algorithm::DEFAULT, Algorithm::EXISTING, Algorithm::MD5, Algorithm::PLAINTEXT, Algorithm::SALT_CHARS, Algorithm::SALT_LENGTH, Algorithm::SHA1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Algorithm

algorithm_from_field, algorithm_from_name, algorithm_name, #gen_salt, secure_compare, #to_64

Methods included from DescendantTracker

#children, #find_child, #inherited

Constructor Details

#initialize(params = {}) ⇒ Bcrypt

Returns a new instance of Bcrypt.



23
24
25
26
27
28
29
# File 'lib/htauth/bcrypt.rb', line 23

def initialize(params = {})
  if existing = (params['existing'] || params[:existing]) then
    @cost = self.class.extract_cost_from_existing_password_field(existing)
  else
    @cost = params['cost'] || params[:cost] || DEFAULT_APACHE_COST
  end
end

Instance Attribute Details

#costObject

Returns the value of attribute cost.



10
11
12
# File 'lib/htauth/bcrypt.rb', line 10

def cost
  @cost
end

Class Method Details

.extract_cost_from_existing_password_field(existing) ⇒ Object



18
19
20
21
# File 'lib/htauth/bcrypt.rb', line 18

def self.extract_cost_from_existing_password_field(existing)
  password = ::BCrypt::Password.new(existing)
  password.cost
end

.handles?(password_entry) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/htauth/bcrypt.rb', line 14

def self.handles?(password_entry)
  return ::BCrypt::Password.valid_hash?(password_entry)
end

Instance Method Details

#encode(password) ⇒ Object



31
32
33
# File 'lib/htauth/bcrypt.rb', line 31

def encode(password)
  ::BCrypt::Password.create(password, :cost => cost)
end

#verify_password?(password, digest) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/htauth/bcrypt.rb', line 35

def verify_password?(password, digest)
  bc = ::BCrypt::Password.new(digest)
  bc.is_password?(password)
end