Module: Authenticate::Crypto::BCrypt

Defined in:
lib/authenticate/crypto/bcrypt.rb

Overview

All crypto providers must implement encrypt(secret) and match?(secret, encrypted)

Instance Method Summary collapse

Instance Method Details

#costObject


17
18
19
# File 'lib/authenticate/crypto/bcrypt.rb', line 17

def cost
  @cost ||= ::BCrypt::Engine::DEFAULT_COST
end

#cost=(val) ⇒ Object


21
22
23
24
25
26
27
# File 'lib/authenticate/crypto/bcrypt.rb', line 21

def cost=(val)
  if val < ::BCrypt::Engine::MIN_COST
    msg = "bcrypt cost cannot be set below the engine's min cost (#{::BCrypt::Engine::MIN_COST})"
    raise ArgumentError.new(msg), msg
  end
  @cost = val
end

#encrypt(secret) ⇒ Object


8
9
10
# File 'lib/authenticate/crypto/bcrypt.rb', line 8

def encrypt(secret)
  ::BCrypt::Password.create secret, cost: cost
end

#match?(secret, encrypted) ⇒ Boolean

Returns:

  • (Boolean)

12
13
14
15
# File 'lib/authenticate/crypto/bcrypt.rb', line 12

def match?(secret, encrypted)
  return false unless encrypted.present?
  ::BCrypt::Password.new(encrypted) == secret
end