Ruby方式为OAuth生成HMAC-SHA1签名 [英] Ruby way to generate a HMAC-SHA1 signature for OAuth

查看:181
本文介绍了Ruby方式为OAuth生成HMAC-SHA1签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个小的ruby程序,通过OAuth玩Twitter,还没有找到正确的方式来执行HMAC-SHA1签名。到目前为止,我搞砸了$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ chomp

但这会输出Twitter拒绝的内容,而不是有效的签名。我实际上是以更糟的方式解决了这个问题,请尽量不要打扰我:

  php -recho rawurlencode(base64_encode hash_hmac('sha1','#{@ signature}','#{llave}',true))); 

最后一个实际工作,我可以去做我的东西。



我想要一些关于如何做到这一点的提示,而不需要回到PHP。我不太喜欢图书馆,而我正在尝试学习一种语言,所以宝石几乎没有问题。



谢谢!

解决方案

以下内容与您的PHP代码相当,尽管我选择不将其包装在一行。



我使用的是宝石ruby-hmac,因为它与1.8以及Ruby 1.9一起使用。如果你专门使用Ruby 1.9,我相信标准库包摘要已经实现了HMAC(但是在1.8版本的包中缺少这个)。确保 gem install ruby​​-hmac

  require'rubygems'
require'base64'
require'cgi'
require'hmac-sha1'

key ='1234'
signature ='abcdef'
hmac = HMAC :: SHA1.new(key)
hmac.update(signature)
放置CGI.escape(Base64.encode64(#{hmac.digest} \\\
))

#等价于:
#php -recho rawurlencode(base64_encode(hash_hmac('sha1','abcdef','1234',true)));更好的是,使用OpenSSL标准库(大多数Linux和MacOS都有开箱即用) 。此代码将适用于Ruby 1.8和1.9:

  require'base64'
require'cgi'
需要'openssl'

key ='1234'
signature ='abcdef'
放置CGI.escape(Base64.encode64(#{OpenSSL :: HMAC.digest(' sha1',key,signature)} \\\
))

#等效于:
#php -recho rawurlencode(base64_encode(hash_hmac('sha1','abcdef' '1234',true)));


I'm writing a small ruby program to play with Twitter over OAuth and have yet to find a right way to do the HMAC-SHA1 signature. So far, I messed around with

Base64.encode64(OpenSSL::HMAC.hexdigest(digest, key, stuff)).chomp

But this outputs something that Twitter rejects, not being a valid signature. I actually solved it in the worse way possible, please try not to slap me:

php -r "echo rawurlencode(base64_encode(hash_hmac('sha1', '#{@signature}', '#{llave}', true)));"

This last one actually works and I can go around doing my stuff.

I'd like some tips on how to do actually do this without reverting back to PHP. I'm not much of a fan of libraries while I'm trying to learn a language, so gems are pretty much out of the question.

Thanks!

解决方案

The following is equivalent to your PHP code, though I chose not to wrap it in a single line.

I'm using the gem ruby-hmac, because it works with 1.8 as well as Ruby 1.9. If you're exclusively using Ruby 1.9 I believe the standard library package 'digest' has HMAC implemented (but this is missing in the 1.8 version of the package). Make sure to gem install ruby-hmac

require 'rubygems'
require 'base64'
require 'cgi'
require 'hmac-sha1'

key = '1234'
signature = 'abcdef'
hmac = HMAC::SHA1.new(key)
hmac.update(signature)
puts CGI.escape(Base64.encode64("#{hmac.digest}\n"))

# equivalent to:
# php -r "echo rawurlencode(base64_encode(hash_hmac('sha1', 'abcdef', '1234', true)));"

Better yet, use the standard library package OpenSSL (which most Linux and MacOS have out of the box). This code will work on Ruby 1.8 and 1.9:

require 'base64'
require 'cgi'
require 'openssl'

key = '1234'
signature = 'abcdef'
puts CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1',key, signature)}\n"))

# equivalent to:
# php -r "echo rawurlencode(base64_encode(hash_hmac('sha1', 'abcdef', '1234', true)));"

这篇关于Ruby方式为OAuth生成HMAC-SHA1签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆