Class: HSign::Digest
Instance Attribute Summary collapse
-
#hmac_key ⇒ Object
Returns the value of attribute hmac_key.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #each_param(&block) ⇒ Object
-
#initialize(secret, salt = nil) ⇒ Digest
constructor
A new instance of Digest.
- #sign(request_params) ⇒ Object
- #verify?(params) ⇒ Boolean
Constructor Details
#initialize(secret, salt = nil) ⇒ Digest
Returns a new instance of Digest.
10 11 12 13 14 15 |
# File 'lib/hsign/digest.rb', line 10 def initialize(secret, salt = nil) @digest = OpenSSL::Digest.new('sha1') @hmac_key = '_hmac' @secret = secret @salt = salt end |
Instance Attribute Details
#hmac_key ⇒ Object
Returns the value of attribute hmac_key.
7 8 9 |
# File 'lib/hsign/digest.rb', line 7 def hmac_key @hmac_key end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/hsign/digest.rb', line 8 def params @params end |
Instance Method Details
#each_param(&block) ⇒ Object
36 37 38 39 |
# File 'lib/hsign/digest.rb', line 36 def each_param &block return false unless @params @params.each(&block) end |
#sign(request_params) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hsign/digest.rb', line 17 def sign(request_params) hmac = OpenSSL::HMAC.new @secret, @digest hmac << @salt if @salt # Normalize all params @params = Rack::Utils.parse_query(Rack::Utils.build_nested_query(request_params.to_hsign_string_values)) @params.delete(hmac_key) @params.to_a.sort_by{|k,v| k.to_s}.each do |k,v| hmac << "#{k}=#{v}" end @params[hmac_key] = hmac.hexdigest end |
#verify?(params) ⇒ Boolean
31 32 33 34 |
# File 'lib/hsign/digest.rb', line 31 def verify?(params) expected = params.delete(hmac_key) expected == sign(params) end |