Class: Devise::Hashable::Strategy::Bcrypt

Inherits:
Base
  • Object
show all
Defined in:
lib/devise/hashable/strategy/bcrypt.rb

Overview

Wrapper for the Devise' standard encryption strategy Bcrypt

Class Method Summary collapse

Class Method Details

.compare(klass, encrypted_password, password) ⇒ Boolean

Verifies a plain-text password against a bcrypt hash.

Parameters:

  • klass (Class)

    the Devise model class

  • encrypted_password (String)

    the stored bcrypt hash

  • password (String)

    the plain-text password to verify

Returns:

  • (Boolean)

    true when the password matches



15
16
17
# File 'lib/devise/hashable/strategy/bcrypt.rb', line 15

def compare(klass, encrypted_password, password)
  Devise::Encryptor.compare(klass, encrypted_password, password)
end

.digest(klass, password) ⇒ String

Hashes a plain-text password with bcrypt.

Parameters:

  • klass (Class)

    the Devise model class, read for stretches

  • password (String)

    the plain-text password to hash

Returns:

  • (String)

    the encoded bcrypt hash



24
25
26
# File 'lib/devise/hashable/strategy/bcrypt.rb', line 24

def digest(klass, password)
  Devise::Encryptor.digest(klass, password)
end

.password_configurations_match?(klass, encrypted_password) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/devise/hashable/strategy/bcrypt.rb', line 28

def password_configurations_match?(klass, encrypted_password)
  current_password_configs = split_password_hash(encrypted_password)
  required_password_configs = base_configs(klass)

  return false if current_password_configs[:stretches] != [required_password_configs[:stretches], 4].max

  true
end

.split_password_hash(encrypted_password) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/devise/hashable/strategy/bcrypt.rb', line 37

def split_password_hash(encrypted_password)
  split_digest = encrypted_password.split("$")

  raise InvalidPasswordHash, "Invalid password hash for Bcrypt" unless split_digest.length == 4

  _, strategy, stretches, _checksum = split_digest

  raise InvalidPasswordHash, "Invalid password hash strategy for Bcrypt" unless strategy == "2a"

  {
    stretches: stretches.to_i
  }
end