You must sign all API requests to ensure security. Alibaba Cloud uses the request signature to verify the identity of the API caller. Simple Application Server implements symmetric encryption with an AccessKey pair to verify the identity of the request sender.

Overview

You must add the signature to the Simple Application Server API request in the following format:

https://Endpoint/?SignatureVersion=1.0&SignatureMethod=HMAC-SHA1&Signature=CT9X0VtwR86fNWSnsc6v8YGOjuE%3D&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf
where:
  • SignatureMethod: the encryption method of the signature string. Set the value to HMAC-SHA1.
  • SignatureVersion: the version of the signature encryption algorithm. Set the value to 1.0.
  • SignatureNonce: a unique, random number used to prevent replay attacks. You must use different random numbers for different requests. We recommend that you use universally unique identifiers (UUIDs).
  • Signature: The signature generated after the request is symmetrically encrypted by using the AccessKey secret.
Calculate the RFC 2104-compliant HMAC-SHA1 value of StringToSign. The Java Base64 encoding method is used in this example.
Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign)) )
Note When you calculate the signature, the key value defined by RFC 2104 is your AccessKey secret followed by an ampersand ( &), which has an ASCII value of 38. For more information, see Create an AccessKey pair.

Step 1: Create and encode a string-to-sign

  1. Create a canonicalized query string by using the request parameters.
    1. Create a canonicalized query string by arranging the request parameters (including all common and operation-specific parameters except Signature) in alphabetical order.
      Note If you use the GET method to send a request, the request parameters are included in the request URL. The request parameters in the URL are placed after a question mark ( ?) and separated by ampersands ( &).
    2. Encode the canonicalized query string in UTF-8. The following table describes the encoding rules.
      Character Encoding rule
      Uppercase letters, lowercase letters, digits, hyphens (-), underscores (_), periods (.),and tildes (~) These characters do not need to be encoded.
      Other characters These characters must be percent encoded in the %XY format. XY represents the ASCII code of the characters in hexadecimal notation. For example, double quotation marks (") are encoded as %22.
      Extended UTF-8 characters These characters are encoded in the %XY%ZA…format.
      Spaces Spaces must be encoded as %20. Do not encode spaces as plus signs (+).
      This encoding method is different from the application/x-www-form-urlencoded MIME encoding algorithm, such as the java.net.URLEncoder class provided by the Java standard library. However, you can apply the encoding algorithm and replace the plus sign (+) in the encoded string with %20, the asterisk (*) with %2A, and the tilde (~) with %7E. You can use the following percentEncode method:
      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;
      }
    3. Connect the encoded parameter names and values by using equal signs (=).
    4. Sort the connected parameter name and value pairs in the order specified in Step i. Then, concatenate the pairs with ampersands (&) to construct the canonicalized query string.
  2. Create a string-to-sign from the encoded canonicalized query string based on the following rules:
    StringToSign=
      HTTPMethod + "&" +
      percentEncode("/") + "&" +
      percentEncode(CanonicalizedQueryString)

    where:

    • HTTPMethod: the HTTP method used to submit a request, such as GET.
    • percentEncode("/"): Encode backslashes (/) as %2F.
    • percentEncode(CanonicalizedQueryString): Encode the canonicalized query string based on the URL encoding rule described in Step 1 and Step 2.

Step 2: Calculate the signature string

  1. Calculate the HMAC value of the string-to-sign as defined in RFC 2104.
    Note Use the SHA1 algorithm to calculate the HMAC value of the string-to-sign. Your AccessKey secret followed by an ampersand ( &) (ASCII code 38) is used as the key for the HMAC calculation.
  2. Encode the HMAC value in Base64 to obtain the signature string
  3. Add the signature string to the request as the Signature parameter.
    Note When the obtained signature value is submitted as the final value of a request parameter, the value must be URL-encoded. The URL-encoded value is similar to other parameters based on the rules defined in RFC 3986.

Signature example

In the following example, the ListInstances API operation is used, and AccessKey ID is testid and AccessKey secret is testsecret. Request URL to be signed:

https://swas.cn-hangzhou.aliyuncs.com?Timestamp=2020-10-23T12%3A46:24Z&Format=XML&AccessKeyId=testid&Action=ListInstances&SignatureMethod=HMAC-SHA1&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2020-06-01&SignatureVersion=1.0

The following signature string calculated by using testsecret& is returned:

OLeaidS1JvxuMvnyHOwuJ+uX5qY=

Add the signature string to the request as the Signature parameter. The following signed URL is obtained:

https://swas.cn-hangzhou.aliyuncs.com?SignatureVersion=1.0&Action=ListInstances&Format=XML&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2020-06-01&AccessKeyId=testid&Signature=OLeaidS1JvxuMvnyHOwuJ+uX5qY=&SignatureMethod=HMAC-SHA1&Timestamp=2020-10-23T12%3A46%3A24Z