edit-icon download-icon

Signature

Last Updated: Jul 11, 2018

Access Key ID and Access Key Secret

  • HiTSDB performs authentication on each API access request. Therefore, each request, whether being sent through HTTP or HTTPS, must contain the signature consisting of an Access Key ID and an Access Key Secret.
  • The Access Key ID and Access Key Secret are officially issued to visitors by Alibaba Cloud (visitors can apply for and manage them at Alibaba Cloud’s official website). The Access Key ID indicates the identity of the visitor.
  • The Access Key Secret is the secret key used to encrypt and verify the signature string on the server. It must be kept confidential and only be available to Alibaba Cloud and the user.

Mechanism

When sending an API call, use the following method to sign the request:

  1. Construct a normalized request string (Canonicalized Query String) using the request parameter.

    1.1. The request parameters are ordered alphabetically by the parameter names (this includes the public request parameters and user-defined parameters for the given request interfaces described in this document, but not the Signature parameter mentioned in public request parameters).

    Note: For a request submitted using the GET method, these parameters constitute the parameter section of the request URI, that is, the section in the URI following the question mark (?) and connected by the ampersand (&).

    1.2. Encode the name and value of each request parameter. The names and values must be URL encoded by using the UTF-8 character set. The URL encoding rules are as follows:

    • English letters A–Z and a–z, digits 0–9, and characters “-“, “_”, “.”, and “~” are not encoded.

    • Other characters are encoded in “%XY” format, with XY representing the characters’ ASCII code in hexadecimal notation. For example, the double quotes (“) are encoded as %22.

    • Encode extended UTF-8 characters in “%XY%ZA…” format.

    • An English space ( ) is encoded as %20 rather than the plus sign (+).

      Note: Generally, libraries that support URL encoding (such as Java’s java.net.URLEncoder) are all encoded according to the rules for the “application/x-www-form-urlencoded” MIME-type. If this encoding method is used, replace the plus signs (+) in the encoded strings with %20, the asterisks (*) with %2A, and change %7E back to the tilde (~) to conform to the encoding rules described above.

    1.3. Connect the encoded parameter names and values with the equal sign (=).

    1.4. Sort the parameter name and value pairs connected by equal signs (=) in alphabetical order, and connect them with ampersands (&) to produce the canonicalized query string.

  2. Follow the following rules to construct the string used for signature calculation by using the Canonicalized Query String constructed in the previous step.

    1. StringToSign=
    2. HTTPMethod + "&" +
    3. percentEncode("/") + "&" +
    4. percentEncode(CanonicalizedQueryString)
    • HTTPMethod: indicates the HTTP method used for request submission, for example, GET.
    • percentEncode(“/”): the coded value for the character “/“ according to the URL encoding rules described in 1.2, namely, “%2F”.
    • percentEncode(CanonicalizedQueryString) indicates the encoded string of the canonicalized query string constructed in Step 1, produced by following the URL encoding rules described in 1.2.
  3. Use the preceding signature sting to calculate the signature’s HMAC value based on RFC2104 definitions.

    Note: The Key used for calculating the signature is the AccessKey Secret held by the user, added with the “&” character (ASCII:38) and is based on the SHA1 hashing.

  4. According to Base64 encoding rules, encode the preceding HMAC value, which gives you the signature value.

  5. Add the obtained signature value to the request parameters as the “Signature” parameter, which completes the request signing process.

    Note: When the obtained signature value is submitted to the HiTSDB server as the final request parameter value, the URL encoding must be performed for the value according to RFC3986 rules like other parameters.

Samples

DescribeHiTSDBInstanceList is used as an example. The request parameter string before signing is:

  1. AccessKeyId=testid&Action=DescribeHiTSDBInstanceList&Format=JSON&RegionId=cn-hangzhou&SignatureMethod=HMAC-SHA1&SignatureNonce=ae5bdbeb-9b44-40a1-8bb4-b40784bff686&SignatureVersion=1.0&Timestamp=2016-01-20T14%3A26%3A15Z&Version=2017-06-01

Thus, the StringToSign is:

  1. GET&%2F&AccessKeyId%3Dtestid&Action%3DDescribeHiTSDBInstanceList&Format%3DJSON&RegionId%3Dcn-hangzhou&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3Dae5bdbeb-9b44-40a1-8bb4-b40784bff686&SignatureVersion%3D1.0&Timestamp%3D2016-01-20T14%253A26%253A15Z&Version%3D2017-06-01

Assume that the Access Key ID is “testid”, the Access Key Secret is “testsecret”, and the key used for HMAC calculation is “testsecret&”. The calculated signature is:

  1. h/ka/jNO+WZv8Tqgo4a75sp6eTs=

With the signature parameter added, the signed request URL is:

  1. http://hitsdb.aliyuncs.com/?AccessKeyId=testid&Action=DescribeHiTSDBInstanceList&Format=JSON&RegionId=cn-hangzhou&SignatureMethod=HMAC-SHA1&SignatureNonce=ae5bdbeb-9b44-40a1-8bb4-b40784bff686&SignatureVersion=1.0&Timestamp=2016-01-20T14%3A26%3A15Z&Version=2017-06-01&Signature=h%2Fka%2FjNO%2BWZv8Tqgo4a75sp6eTs%3D

The Java sample code for calling the DescribeHiTSDBInstanceList API is as follows:

  1. public static void hitsdbOpenAPI() throws NoSuchAlgorithmException, InvalidKeyException, IOException {
  2. //AccessKet and SecretKey
  3. String accessKey = "testid";
  4. String accessSecret = "testsecret";
  5. //Public parameters
  6. Map<String, String> parameters = new TreeMap<String, String>();
  7. parameters.put("Format", "JSON");
  8. parameters.put("Action", "DescribeHiTSDBInstanceList");//Calls the DescribeDrdsInstances API.
  9. parameters.put("Version", "2017-06-01");
  10. parameters.put("AccessKeyId", accessKey);
  11. parameters.put("SignatureMethod", "HMAC-SHA1");
  12. parameters.put("Timestamp", getISO8601Time());
  13. parameters.put("SignatureVersion", "1.0");
  14. parameters.put("SignatureNonce", UUID.randomUUID().toString());
  15. parameters.put("RegionId", "cn-hangzhou");
  16. StringBuilder paramStr = new StringBuilder();
  17. //Concatenates the request parameters.
  18. for(Map.Entry<String, String> entry : parameters.entrySet()) {
  19. paramStr.append(percentEncode(entry.getKey())).append("=").append(percentEncode(entry.getValue())).append("&");
  20. }
  21. paramStr.deleteCharAt(paramStr.length()-1);
  22. //Calculates the signature.
  23. StringBuilder stringToSign = new StringBuilder();
  24. stringToSign.append("GET").append("&").append(percentEncode("/"))
  25. .append("&").append(percentEncode(paramStr.toString()));
  26. Mac mac = Mac.getInstance("HmacSHA1");
  27. mac.init(new SecretKeySpec((accessSecret + "&").getBytes("UTF-8"),"HmacSHA1"));
  28. byte[] signData = mac.doFinal(stringToSign.toString().getBytes("UTF-8"));
  29. String signStr = Base64Helper.encode(signData);
  30. //Concatenates the URL.
  31. String requestUrl = "http://hitsdb.aliyuncs.com/?" + paramStr.toString() +
  32. "&Signature=" + percentEncode(signStr);
  33. //Ready to send the HTTP request.
  34. URL url = new URL(requestUrl);
  35. HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
  36. httpConn.setRequestMethod("GET");
  37. httpConn.setDoOutput(true);
  38. httpConn.setDoInput(true);
  39. httpConn.setUseCaches(false);
  40. httpConn.connect();
  41. InputStream content = httpConn.getInputStream();
  42. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  43. byte[] buff = new byte[1024];
  44. while(true) {
  45. final int read = content.read(buff);
  46. if(read == -1) break;
  47. outputStream.write(buff,0,read);
  48. }
  49. System.out.println(new String(outputStream.toByteArray()));;
  50. }
  51. public static String percentEncode(String value) throws UnsupportedEncodingException{
  52. return value != null ? URLEncoder.encode(value, "UTF-8").replace("+", "%20")
  53. .replace("*", "%2A").replace("%7E", "~") : null;
  54. }
  55. static String getISO8601Time() {
  56. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  57. df.setTimeZone(new SimpleTimeZone(0, "GMT"));
  58. return df.format(new Date());
  59. }
  60. }
Thank you! We've received your feedback.