Builds the canonical string for signing requests. This strips out all ’&’, ’?’, and ’=’ from the query string to be signed. The parameters in the path passed in must already be sorted in case-insensitive alphabetical order and must not be url encoded.
@param [String] params the params that will be sorted and encoded as a canonical string. @param [String] host the hostname of the API endpoint. @param [String] method the HTTP method that will be used to submit the params. @param [String] base the URI path that this information will be submitted to. @return [String] the canonical request description string.
# File lib/AWS.rb, line 63
63: def AWS.canonical_string(params, host, method="POST", base="/")
64: # Sort, and encode parameters into a canonical string.
65: sorted_params = params.sort {|x,y| x[0] <=> y[0]}
66: encoded_params = sorted_params.collect do |p|
67: encoded = (CGI::escape(p[0].to_s) +
68: "=" + CGI::escape(p[1].to_s))
69: # Ensure spaces are encoded as '%20', not '+'
70: encoded = encoded.gsub('+', '%20')
71: # According to RFC3986 (the scheme for values expected by signing requests), '~'
72: # should not be encoded
73: encoded = encoded.gsub('%7E', '~')
74: end
75: sigquery = encoded_params.join("&")
76:
77: # Generate the request description string
78: req_desc =
79: method + "\n" +
80: host + "\n" +
81: base + "\n" +
82: sigquery
83:
84: end
Encodes the given string with the secret_access_key by taking the hmac-sha1 sum, and then base64 encoding it. Optionally, it will also url encode the result of that to protect the string if it‘s going to be used as a query string parameter.
@param [String] secret_access_key the user‘s secret access key for signing. @param [String] str the string to be hashed and encoded. @param [Boolean] urlencode whether or not to url encode the result., true or false @return [String] the signed and encoded string.
# File lib/AWS.rb, line 95
95: def AWS.encode(secret_access_key, str, urlencode=true)
96: digest = OpenSSL::Digest::Digest.new('sha1')
97: b64_hmac =
98: Base64.encode64(
99: OpenSSL::HMAC.digest(digest, secret_access_key, str)).gsub("\n","")
100:
101: if urlencode
102: return CGI::escape(b64_hmac)
103: else
104: return b64_hmac
105: end
106: end