Skip to content

Instantly share code, notes, and snippets.

@mloughran
Created April 23, 2010 17:56
Show Gist options
  • Save mloughran/376898 to your computer and use it in GitHub Desktop.
Save mloughran/376898 to your computer and use it in GitHub Desktop.
# encoding: UTF-8
require 'digest/md5'
puts Digest::MD5.hexdigest("asdf") # => "912ec803b2ce49e4a541068d495ab570"
puts Digest::MD5.hexdigest("åß∂ƒ") # => "0d65ab8f1db3344230f46420e56e465f"
# Test script to generate pusherapp auth_signature given secret and string
#
# Dependencies
# gem install ruby-hmac
#
require 'rubygems'
require 'hmac-sha2'
secret = '7ad3773142a6692b25b8'
string_to_sign = "POST\n/apps/3/channels/test_channel/events\nauth_key=278d425bdf160c739803&auth_timestamp=1272044395&auth_version=1.0&body_md5=7b3d404f5cde4a0b9b8fb4789a0098cb&name=foo"
hmac = HMAC::SHA256.hexdigest(secret, string_to_sign)
puts hmac
# >> 309fc4be20f04e53e011b00744642d3fe66c2c7c5686f35ed6cd2af6f202e445
@ethagnawl
Copy link

This was helpful. +1

@sayyedhanif
Copy link

using javascript or nodejs

@sayyedhanif
Copy link

how to calculate auth_timestamp (in js), please let me know

@gawin
Copy link

gawin commented Jun 16, 2016

Lua

local crypto   = require("crypto")
local hmac     = require("crypto.hmac")

secret_key = '7ad3773142a6692b25b8'
auth_key = '278d425bdf160c739803'
auth_timestamp = '1353088179'
auth_version = '1.0'
medhod = 'POST'
path = '/apps/3/events'
body_str = "{\"name\":\"foo\",\"channels\":[\"project-3\"],\"data\":\"{\\\"some\\\":\\\"data\\\"}\"}"
body_md5 = crypto.digest('md5', body_str)
auth_signing = medhod .. '\n' .. path .. '\nauth_key=' .. auth_key  .. '&auth_timestamp=' .. auth_timestamp .. '&auth_version=1.0&body_md5=' .. body_md5
auth_signature = hmac.digest("sha256", auth_signing, secret_key)

print(auth_signature)

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