Sha256: d085b771747eb01e1d5eb64a5f9aa9b3201041c6bdf5b577a9e94248d22f4112

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'digest/sha2'
require 'bcrypt'

module Challah
  # Handles all encryption, hashing and comparison necessary for tokens and passwords.
  class Encrypter
    attr_accessor :cost, :joiner

    # The number of times to hash the given password.
    def cost
      @cost ||= 10
    end

    # Used to join multiple parameters for a given encrypt command.
    def joiner
      @joiner ||= "|"
    end

    # 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.
    def hash(*tokens)
      result = tokens.flatten.join(joiner)
      cost.times { result = Digest::SHA512.hexdigest(result) }
      result
    end

    def md5(*tokens)
      Digest::MD5.hexdigest(tokens.flatten.join(joiner))
    end

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

    # Returns true if the the bcrypted value of a is equal to b
    def compare(crypted_string, plain_string)
      BCrypt::Password.new(crypted_string).is_password?(plain_string)
    rescue BCrypt::Errors::InvalidHash
      false
    end

    class << self
      # Setup some pass through convenience methods that use default options
      %w( hash md5 encrypt compare ).each do |f|
        class_eval "def #{f}(*args); new.#{f}(*args); end"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
challah-1.2.1 lib/challah/encrypter.rb
challah-1.2.0 lib/challah/encrypter.rb
challah-1.2.0.rc lib/challah/encrypter.rb
challah-1.1.1 lib/challah/encrypter.rb