First, '\n'
in Ruby means literally \
and n
. To represent newline, you need to use double-quoted string: "\n"
.
The order of parameter of Digest::HMAC.hexdigest
is different from python version. And according to the Digest::HMAC
documetation, its usage is discouraged. Use OpenSSL:HMAC
instead.
So the ruby script should be read as:
require 'base64'
require 'openssl'
key = ''
text = "GET\n\n\nSat, 22 Mar 2014 13:49:42 +0000\n"
# hex = Digest::HMAC.hexdigest(text, key, Digest::SHA1) # <-- discouraged
hex = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), key, text)
p Base64.urlsafe_encode64(hex)
# => "ZmUxN2YxMDQxMTdlODg3MDljMTRjYjY0ZGU1NDk1MjgwNzI2Yjk1Zg=="