just want to generate the HMAC-SHA1 signature

1,053 views
Skip to first unread message

fairchild

unread,
Oct 9, 2008, 6:52:04 AM10/9/08
to oauth-ruby
Hello,
I'm trying to implement a small subset of the Oauth gem in . I am
getting stuck at creating the signature tho. I have read thru the
source code but am losing the thread somewhere. I am hoping perhaps
someone could help me understand why the following code does not
create the signature specified in the oath specs?

def signature(base_string, secret)
Base64.encode64(HMAC::SHA1.digest(escape(secret),
base_string)).chomp.gsub(/\n/,'')
end

def escape(value)
CGI.escape(value.to_s).gsub("%7E", '~').gsub("+", "%20")
end

for instance, the oauth specs at http://wiki.oauth.net/TestCases#note1
show that
signature('bs', 'cs') should equal 'egQqG5AJep5sJ7anhXju1unge2I='
but I am getting 'VZVjXceV7JgPq/dOTnNmEfO0Fv8='

I've gone thru the gem source code, and can't for the life of me
figure out what I'm leaving out? Any ideas?
Thanks,
Michael

Pelle Braendgaard

unread,
Oct 9, 2008, 12:40:50 PM10/9/08
to oauth...@googlegroups.com
The problem is you are missing out on the '&' (which should not be
escaped) separating the 2 secrets.

def signature(base_string, consumer_secret,token_secret='')
secret="#{escape(consumer_secret)}&#{escape(token_secret)}"
Base64.encode64(HMAC::SHA1.digest(secret,base_string)).chomp.gsub(/\n/,'')
end

This returns the correct signatures:

irb(main):037:0> signature('bs', 'cs','ts')
=> "VZVjXceV7JgPq/dOTnNmEfO0Fv8="
irb(main):038:0> signature('bs', 'cs')
=> "egQqG5AJep5sJ7anhXju1unge2I="

Pelle

--
http://agree2.com - Reach Agreement!
http://extraeagle.com - Solutions for the electronic Extra Legal world
http://stakeventures.com - Bootstrapping blog

fairchild

unread,
Oct 9, 2008, 4:06:14 PM10/9/08
to oauth-ruby
Thanks very much Pelle. I was going crazy trying to figure out what I
was missing. So simple in the end. It works perfectly now.
Thanks again,
~Michael
> > for instance, the oauth specs athttp://wiki.oauth.net/TestCases#note1
> > show that
> > signature('bs', 'cs')  should equal 'egQqG5AJep5sJ7anhXju1unge2I='
> >  but I am getting 'VZVjXceV7JgPq/dOTnNmEfO0Fv8='
>
> >  I've gone thru the gem source code, and can't for the life of me
> > figure out what I'm leaving out?  Any ideas?
> >  Thanks,
> >  Michael
>
> --http://agree2.com- Reach Agreement!http://extraeagle.com- Solutions for the electronic Extra Legal worldhttp://stakeventures.com- Bootstrapping blog

compraker

unread,
Nov 14, 2008, 2:10:27 AM11/14/08
to oauth-ruby
hi there -

what libraries did you include at the top. I'm getting:
`signature': uninitialized constant HMAC (NameError)
> > --http://agree2.com-Reach Agreement!http://extraeagle.com-Solutions for the electronic Extra Legal worldhttp://stakeventures.com-Bootstrapping blog

choonkeat

unread,
Nov 14, 2008, 11:42:00 AM11/14/08
to oauth...@googlegroups.com
Try installing the "ruby-hmac" gem

Cheers
--
choonkeat
Reply all
Reply to author
Forward
0 new messages