module HasChecksum

Constants

VERSION

Public Class Methods

included(klass) click to toggle source
# File lib/has_checksum.rb, line 7
def self.included(klass)
  klass.extend ClassMethods
  klass.class_eval do

    private

    def digest_string(methods)
      methods.map { |name| public_send(name) }.join("")
    end

    def calculate_signature(digest, value, options = {})
      key = case options[:key]
            when Symbol
              raise "key option refers to an unknown method '#{options[:key]}'" unless respond_to?(options[:key])
              send(options[:key])
            when Proc
              options[:key][]
            else
              options[:key]
            end

      hmac = OpenSSL::HMAC.new(key.to_s, digest)
      hmac << value

      case options[:format]
      when :binary, "binary"
        hmac.digest
      else
        hmac.hexdigest
      end
    end

    def calculate_checksum(klass, value, options = {})
      case options[:format]
      when :binary, "binary"
        klass.digest(value)
      when :base64, "base64"
        klass.base64digest(value)
      when :bubblebabble, "bubblebabble"
        klass.bubblebabble(value)
      else
        klass.hexdigest(value)
      end
    end
  end
end

Public Instance Methods

calculate_checksum(klass, value, options = {}) click to toggle source
# File lib/has_checksum.rb, line 39
def calculate_checksum(klass, value, options = {})
  case options[:format]
  when :binary, "binary"
    klass.digest(value)
  when :base64, "base64"
    klass.base64digest(value)
  when :bubblebabble, "bubblebabble"
    klass.bubblebabble(value)
  else
    klass.hexdigest(value)
  end
end
calculate_signature(digest, value, options = {}) click to toggle source
# File lib/has_checksum.rb, line 17
def calculate_signature(digest, value, options = {})
  key = case options[:key]
        when Symbol
          raise "key option refers to an unknown method '#{options[:key]}'" unless respond_to?(options[:key])
          send(options[:key])
        when Proc
          options[:key][]
        else
          options[:key]
        end

  hmac = OpenSSL::HMAC.new(key.to_s, digest)
  hmac << value

  case options[:format]
  when :binary, "binary"
    hmac.digest
  else
    hmac.hexdigest
  end
end
digest_string(methods) click to toggle source
# File lib/has_checksum.rb, line 13
def digest_string(methods)
  methods.map { |name| public_send(name) }.join("")
end