signature.rb 521 B

1234567891011121314
  1. require "openssl"
  2. require "base64"
  3. key = ["943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881"].pack("H*")
  4. salt = ["520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5"].pack("H*")
  5. path = "/rs:fit:300:300/plain/http://img.example.com/pretty/image.jpg"
  6. digest = OpenSSL::Digest.new("sha256")
  7. # You can trim padding spaces to get good-looking url
  8. hmac = Base64.urlsafe_encode64(OpenSSL::HMAC.digest(digest, key, "#{salt}#{path}")).tr("=", "")
  9. signed_path = "/#{hmac}#{path}"
  10. puts signed_path