Skip to content

Instantly share code, notes, and snippets.

@kovacshuni
Last active March 12, 2022 15:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kovacshuni/6f10a84f828a2bce0f88 to your computer and use it in GitHub Desktop.
Save kovacshuni/6f10a84f828a2bce0f88 to your computer and use it in GitHub Desktop.
Ruby HMAC-SHA1 digest creation
require 'base64'
require 'cgi'
require 'openssl'
base = 'POST&https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521'
key = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw&LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'
puts CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', key, base)}\n"))
@pachun
Copy link

pachun commented Jan 7, 2022

If I may, for anyone that may find this, I tweaked this slightly to make it work the same as is being described in the Twitter API docs here (which I believe the original author had also been following along with a couple years ago):

require 'base64'
require 'openssl'

base = 'POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521'

key = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw&LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'

puts Base64.encode64(OpenSSL::HMAC.digest('sha1', key, base))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment