module Hashit::Digests

Constants

HASHING_DIGESTS

Public Instance Methods

current_time() click to toggle source
# File lib/hashit/digests.rb, line 24
def current_time
  d = DateTime.now
  ts = d.strftime("%s").to_i
  ts - ((d.minute % 30) * 60) - d.second
end
did_match?(hash, key, text) click to toggle source
# File lib/hashit/digests.rb, line 77
def did_match? hash, key, text
  type = get_hash_type hash.split('|').first
  fn = type.to_s.sub("timed", "previous").to_sym
  raise ArgumentError, "invalid hash" if type.nil? || !Hashit.respond_to?(fn)
  new_hash = Hashit.send fn, key, text
  new_hash == hash
end
generate_hash(digest, key, text) click to toggle source
# File lib/hashit/digests.rb, line 34
def generate_hash digest, key, text
  text = prepare text

  if key.is_a? Array
    key.reduce(text) do |text, k|
      k = prepare k
      OpenSSL::HMAC.hexdigest(digest, k, text)
    end
  else
    key = Hashit.prepare key
    OpenSSL::HMAC.hexdigest(digest, key, text)
  end
end
last_time() click to toggle source
# File lib/hashit/digests.rb, line 30
def last_time
  current_time - (30 * 60)
end
matches?(hash, key, text) click to toggle source
# File lib/hashit/digests.rb, line 70
def matches? hash, key, text
  type = get_hash_type hash.split('|').first
  raise ArgumentError, "invalid hash" if type.nil? || !Hashit.respond_to?(type)
  new_hash = Hashit.send type, key, text
  new_hash == hash
end
prepare(obj) click to toggle source
# File lib/hashit/digests.rb, line 48
def prepare obj
  return obj if obj.is_a? String

  if obj.is_a? Array
    obj = prepare_array obj
  elsif obj.nil?
    raise ArgumentError, "Nil passed in as a text parameter"
  elsif obj.respond_to? :to_s
      obj = obj.to_s
  else
    raise ArgumentError, "Parameter #{obj} cannot be converted to string"
  end

  obj
end
prepare_array(arr) click to toggle source
# File lib/hashit/digests.rb, line 64
def prepare_array arr
  arr.reduce("") do |str, el|
    str += prepare el
  end
end

Private Instance Methods

get_hash_type(segment) click to toggle source
# File lib/hashit/digests.rb, line 86
def get_hash_type segment
  i = segment[1..-1].to_i
  return nil if segment[1..-1] != i.to_s
  type = HASHING_DIGESTS[i]
  type = "timed_#{type}" if segment[0] == 't'
  type.to_sym
end