Sha256: 697105d66c8c152a98fe9ed1a2157b91da64ad6018680f5855a965bfa125b519

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require 'uri'
require 'base64'
require 'hmac-sha1'

require 'aliyun/signature_methods/base'

module Aliyun
  module SignatureMethods
    class HmacSha1 < Base
      NAME = "HMAC-SHA1".freeze
      VERSION = "1.0".freeze

      def generate(params, service)
        calculate_signature("#{service.access_key_secret}&", string_to_sign(params, service))
      end

      private

      def calculate_signature(key, string_to_sign)
        hmac = HMAC::SHA1.new(key)
        hmac.update(string_to_sign)
        Base64.encode64(hmac.digest).gsub("\n", '')
      end

      def string_to_sign(params, service)
        "#{service.http_method}#{service.separator}#{safe_encode('/')}#{service.separator}#{safe_encode(canonicalized_query_string(params, service))}"
      end

      def canonicalized_query_string(params, service)
        params.keys.sort.map do |k|
          "%s=%s" % [safe_encode(k.to_s), safe_encode(params[k.to_s])]
        end.join(service.separator)
      end

      def safe_encode(s)
        URI.encode_www_form_component(s).gsub(/\+/,'%20').gsub(/\*/,'%2A').gsub(/%7E/,'~')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aliyun-sdk-core-0.1.5 lib/aliyun/signature_methods/hmac_sha1.rb
aliyun-sdk-core-0.1.4 lib/aliyun/signature_methods/hmac_sha1.rb
aliyun-sdk-core-0.1.3 lib/aliyun/signature_methods/hmac_sha1.rb