hmac_signature 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hmac_signature.rb +33 -0
  2. metadata +46 -0
@@ -0,0 +1,33 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
4
+ class HmacSignature
5
+ def initialize(key)
6
+ @key = key
7
+ end
8
+
9
+ def sign(verb, host, path, query_params)
10
+ # sort the params alphabetically by key and join
11
+ # them with '='
12
+ sorted_query_params = query_params.sort.map do |param|
13
+ param.join("=")
14
+ end # => ["user=mat", "tag=ruby"]
15
+
16
+ # join the sorted params into one string
17
+ canonicalized_params = sorted_query_params.join("&")
18
+ # => "user=mat&tag=ruby"
19
+
20
+ # Construct the string to sign by concatenating the
21
+ # various parts of the request.
22
+ string_to_sign = verb + host + path + canonicalized_params
23
+
24
+ # Construct an hmac signer using our secret key
25
+ digest = OpenSSL::HMAC.digest('sha256', @key, string_to_sign)
26
+ # digest = Digest::SHA256.new(@key)
27
+ # digest.update(string_to_sign)
28
+
29
+ # Encrypt the string and Base64 encode it (to
30
+ # make it cleaner when putting it into the request).
31
+ Base64.encode64(digest).chomp
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hmac_signature
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Dix
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple support class for generating http based request signatures
15
+ email: info@evrium.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/hmac_signature.rb
21
+ homepage: http://rubygems.org/gems/hmac_signature
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.21
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Request signature generator
45
+ test_files: []
46
+ has_rdoc: