Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jjanauskas/3263765 to your computer and use it in GitHub Desktop.
Save jjanauskas/3263765 to your computer and use it in GitHub Desktop.
This example calculates request MAC according to "HTTP Authentication: MAC Access Authentication (draft 00)
# author: Justas Janauskas,
# Aug 5, 2012
#
# This example calculates request MAC according to "HTTP Authentication: MAC Access Authentication (draft 00)"
# example in section 1.2
#
# ref: http://tools.ietf.org/pdf/draft-ietf-oauth-v2-http-mac-00.pdf
require 'base64'
require 'openssl'
normalized_request_string = [
'264095:dj83hs9s',
'GET',
'/resource/1?b=1&a=2',
'example.com',
80,
nil,
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 \"SLDJd4mg43cjQfElUs3Qub4L6xE=\""
# output:
# I get "SLDJd4mg43cjQfElUs3Qub4L6xE=", example has "SLDJd4mg43cjQfElUs3Qub4L6xE="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment