Signature mechanism
Updated at:
To ensure the security of API calls, Alibaba Cloud uses a signature to verify the identity of the sender for each API request. You must include signature information in each request that you submit over HTTP or HTTPS.
Overview
For RPC APIs, you must add a signature to the query string of the API request in the following format.
https://Endpoint/?SignatureVersion=1.0&SignatureMethod=HMAC-SHA1&Signature=CT9X0VtwR86fNWSnsc6v8YGOjuE%3D&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cfThe parameters are as follows:
- SignatureMethod: The signature method. The value must be HMAC-SHA1.
- SignatureVersion: The version of the signature algorithm. The current version is 1.0.
- SignatureNonce: A unique random number used to prevent replay attacks. You must use a different random number for each request. We recommend that you use a universally unique identifier (UUID).
- Signature: The signature string that is generated using your AccessKey secret.
The signature algorithm follows the RFC 2104 HMAC-SHA1 specification. The algorithm uses your AccessKey secret to calculate the HMAC value of the encoded and sorted request string. This value is the signature. The signature is derived from the request parameters. Because the content of each API request is different, the resulting signature is also different. Follow the steps in this topic to calculate the signature.
Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(
StringToSign)) )Step 1: Create the string to sign
- Use the request parameters to create a canonicalized query string.
- Sort all request parameters in alphabetical order by parameter name. The parameters include common request parameters and API-specific parameters, but not the `Signature` parameter. Note If you submit a request using the GET method, these parameters are the part of the request URI that follows the question mark (?) and are connected by ampersands (&).
- URL-encode the names and values of the sorted request parameters in UTF-8. For the encoding rules, see the following table.
Character Encoding method A-Z, a-z, 0-9, and the characters -, _, ., ~ Do not encode. Other characters Encode as %XY, whereXYis the hexadecimal representation of the character's ASCII code. For example, a double quotation mark (") is encoded as%22.Extended UTF-8 characters Encode as %XY%ZA….Space Encode as %20, not as a plus sign (+).This encoding method is different from the `application/x-www-form-urlencoded` Multipurpose Internet Mail Extensions (MIME) format encoding algorithm. For example, it differs from the implementation ofjava.net.URLEncoderin the Java standard library. To get the encoded string described by these rules, you can first encode using a standard library method. Then, replace the plus signs (+) with%20, asterisks (*) with%2A, and%7Eback to tildes (~). This algorithm can be implemented with the followingpercentEncodemethod:private static final String ENCODING = "UTF-8"; private static String percentEncode(String value) throws UnsupportedEncodingException { return value != null ? URLEncoder.encode(value, ENCODING).replace("+", "%20").replace("*", "%2A").replace("%7E", "~") : null; } - Connect each encoded parameter name and its value with an equal sign (=).
- Connect the parameter pairs with ampersands (&) in the same order as in the previous step to create the canonicalized query string.
- Sort all request parameters in alphabetical order by parameter name. The parameters include common request parameters and API-specific parameters, but not the `Signature` parameter.
- Create the string-to-sign from the canonicalized query string that you created in the previous step. The format is as follows:
StringToSign= HTTPMethod + “&” + percentEncode(“/”) + ”&” + percentEncode(CanonicalizedQueryString)Where:
- HTTPMethod is the HTTP method used to submit the request, such as GET.
- percentEncode("/") is the result of encoding the forward slash (/) character based on the URL encoding rules in the previous step. The encoded value is %2F.
- percentEncode(CanonicalizedQueryString) is the result of encoding the canonicalized query string from the previous step based on the specified URL encoding rules.
Step 2: Calculate the signature value
- Calculate the HMAC value of the string-to-sign (`StringToSign`) as defined in RFC 2104. Note The key used for the signature calculation is your AccessKey secret with an ampersand (&) character (ASCII code 38) appended. The hash algorithm is SHA1.
- Encode the HMAC value in Base64 to obtain the signature string (`Signature`).
- Add the signature string to the request as the Signature parameter. Note When you add the signature string as the final request parameter, it must be URL-encoded in the same way as other parameters, as specified in RFC 3986.
Example
This section uses the DescribeRegions API call as an example. Assume that the AccessKeyId is testid and the AccessKey Secret is testsecret. The request URL before signing is as follows:
http://cmn.aliyuncs.com/?Timestamp=2016-02-23T12%3A46:24Z&Format=XML&AccessKeyId=testid&Action=DescribeRegions&SignatureMethod=HMAC-SHA1&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2014-05-26&SignatureVersion=1.0If you use testsecret& as the key, the calculated signature is:
OLeaidS1JvxuMvnyHOwuJ+uX5qY=Add the signature to the request URL as the Signature parameter. The final request URL is:
http://cmn.aliyuncs.com/?SignatureVersion=1.0&Action=DescribeRegions&Format=XML&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2014-05-26&AccessKeyId=testid&Signature=OLeaidS1JvxuMvnyHOwuJ+uX5qY=&SignatureMethod=HMAC-SHA1&Timestamp=2016-02-23T12%3A46%3A24ZIs this page helpful?