Module: Negroni::Encryptor

Defined in:
lib/negroni/encryptor.rb

Overview

Handles password encryption and digestion

Class Method Summary collapse

Class Method Details

.compare(klass, hashed_password, password) ⇒ Object

Compare two passwords

Parameters:

  • klass (Class)

    the class for which to digest

  • hashed_password (String)

    the hashed password to compare

  • password (String)

    the password to compare with ‘hashed_password`



24
25
26
27
28
29
30
31
32
# File 'lib/negroni/encryptor.rb', line 24

def compare(klass, hashed_password, password)
  return false if hashed_password.blank?

  bcrypt   = ::BCrypt::Password.new(hashed_password)
  password = "#{password}#{klass.pepper}" if klass.pepper.present?
  password = ::BCrypt::Engine.hash_secret(password, bcrypt.salt)

  Negroni.secure_compare(password, hashed_password)
end

.digest(klass, password) ⇒ Object

Digest a password

Parameters:

  • klass (Class)

    the class for which to digest

  • password (String)

    the password to digest



13
14
15
16
17
# File 'lib/negroni/encryptor.rb', line 13

def digest(klass, password)
  password = "#{password}#{klass.pepper}" if klass.pepper.present?

  ::BCrypt::Password.create(password, cost: klass.stretches).to_s
end