class Challah::Encrypter

Handles all encryption, hashing and comparison necessary for tokens and passwords.

Attributes

cost[RW]
joiner[RW]

Public Class Methods

compare(*args) click to toggle source
# File lib/challah/encrypter.rb, line 42
def self.compare(*args)
  new().compare(*args)
end
encrypt(*args) click to toggle source
# File lib/challah/encrypter.rb, line 46
def self.encrypt(*args)
  new().encrypt(*args)
end
hash(*args) click to toggle source
# File lib/challah/encrypter.rb, line 50
def self.hash(*args)
  new().hash(*args)
end
md5(*args) click to toggle source
# File lib/challah/encrypter.rb, line 54
def self.md5(*args)
  new().md5(*args)
end

Public Instance Methods

compare(crypted_string, plain_string) click to toggle source

Returns true if the the bcrypted value of a is equal to b

# File lib/challah/encrypter.rb, line 36
def compare(crypted_string, plain_string)
  BCrypt::Password.new(crypted_string).is_password?(plain_string)
rescue BCrypt::Errors::InvalidHash
  false
end
encrypt(secret) click to toggle source
# File lib/challah/encrypter.rb, line 31
def encrypt(secret)
  BCrypt::Password.create(secret, cost: cost)
end
hash(*tokens) click to toggle source

Passwords and secure objects are encrypted (hashed) in a one-way technique. This way any item stored in the database can never be reversed into an actual password.

# File lib/challah/encrypter.rb, line 21
def hash(*tokens)
  result = tokens.flatten.join(joiner)
  cost.times { result = Digest::SHA512.hexdigest(result) }
  result
end
md5(*tokens) click to toggle source
# File lib/challah/encrypter.rb, line 27
def md5(*tokens)
  Digest::MD5.hexdigest(tokens.flatten.join(joiner))
end