Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jjanauskas/3263677 to your computer and use it in GitHub Desktop.
Save jjanauskas/3263677 to your computer and use it in GitHub Desktop.
This example calculates request MAC according to "HTTP Authentication: MAC Access Authentication (draft 01)"
# author: Justas Janauskas,
# Aug 5, 2012
#
# Where is mistake?
#
# This example calculates request MAC according to "HTTP Authentication: MAC Access Authentication (draft 01)"
# example in section 1.1
#
# ref: http://tools.ietf.org/pdf/draft-ietf-oauth-v2-http-mac-01.pdf
require 'base64'
require 'openssl'
normalized_request_string = [
1336363200,
'dj83hs9s',
'GET',
'/resource/1?b=1&a=2',
'example.com',
80,
nil,
nil,
].join("\n")
puts normalized_request_string
hmac = OpenSSL::HMAC.digest(
OpenSSL::Digest::SHA1.new,
'489dks293j39',
normalized_request_string
)
mac = Base64.encode64(hmac).gsub(/\n/, '')
puts "I get \"#{mac}\", example has \"bhCQXTVyfj5cmA9uKkPFx1zeOXM=\""
# output:
# I get "4KQnUNWWNQwCu4rMV6/U8W6FVBw=", example has "bhCQXTVyfj5cmA9uKkPFx1zeOXM="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment