Sign signatures
Every API request to Blockchain as a Service (BaaS) must include a signature. Alibaba Cloud uses this signature to verify your identity and protect against unauthorized access.
If you use an Alibaba Cloud SDK, the SDK handles request signing automatically. Only follow this guide if you are calling the BaaS API directly over HTTP or HTTPS without an SDK.
Key concepts
Before working through the signing steps, familiarize yourself with the terms used in this document.
| Term | Description |
|---|---|
SignatureMethod | The hash algorithm used to calculate the signature. BaaS supports HMAC-SHA1. |
SignatureVersion | The signature algorithm version. The current version is 1.0. |
SignatureNonce | A unique random number included in each request to prevent replay attacks. Use a different value for every request. Universally unique identifiers (UUIDs) are recommended. |
Signature | The Base64-encoded HMAC-SHA1 value calculated from your AccessKey Secret and the canonical request string. |
StringToSign | An intermediate string constructed from the HTTP method and the canonicalized query string, used as input to the HMAC-SHA1 algorithm. |
percentEncode | A URL-encoding function that encodes characters according to the rules in this document. It differs slightly from standard application/x-www-form-urlencoded encoding. |
CanonicalizedQueryString | The alphabetically sorted, percent-encoded query string derived from all request parameters (excluding Signature). |
How it works
A signed BaaS RPC request URL has this structure:
https://<Endpoint>/?SignatureVersion=1.0&SignatureMethod=HMAC-SHA1&Signature=CT9X0VtwR86fNWSnsc6v8YGOjuE%3D&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cfThe signing process has two stages:
Construct `StringToSign` — sort and encode all request parameters into a canonical string.
Calculate the signature — run HMAC-SHA1 on
StringToSignusing your AccessKey Secret as the key, then Base64-encode the result.
The formula is:
Signature = Base64( HMAC-SHA1( AccessKey Secret + "&", UTF-8-Encoding-Of(StringToSign) ) )The signing algorithm follows RFC 2104 (HMAC) and uses HMAC-SHA1. Different requests produce different signatures because the parameters differ.
Sign a request
Step 1: Sort and encode the request parameters
Collect all request parameters except
Signature. This includes common parameters such asSignatureMethod,SignatureVersion,SignatureNonce,Timestamp,AccessKeyId, and any API-specific parameters.Sort the parameters alphabetically by parameter name. Example — unsorted parameters for
DescribeFabricOrganization:Action=DescribeFabricOrganization Timestamp=2018-12-23T12:46:24Z Format=XML AccessKeyId=testid SignatureMethod=HMAC-SHA1 SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf Version=2018-12-21 SignatureVersion=1.0After sorting alphabetically by parameter name:
AccessKeyId=testid Action=DescribeFabricOrganization Format=XML SignatureMethod=HMAC-SHA1 SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf SignatureVersion=1.0 Timestamp=2018-12-23T12%3A46%3A24Z Version=2018-12-21Percent-encode each parameter name and value using the rules below. Java reference implementation:
This encoding differs from
application/x-www-form-urlencoded(used byjava.net.URLEncoder). If you useURLEncoder, apply these replacements afterward:+→%20,*→%2A,%7E→~.Encoding rules:
Character set Encoding A–Z,a–z,0–9,-,_,.,~No encoding — pass through as-is Space %20(not+)All other characters %XYwhereXYis the hexadecimal ASCII value (e.g.,"→%22)Extended UTF-8 characters %XY%ZA…(multi-byte hex sequences)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; }Join each encoded name–value pair with
=, then concatenate all pairs with&to form theCanonicalizedQueryString. Example result:AccessKeyId=testid&Action=DescribeFabricOrganization&Format=XML&SignatureMethod=HMAC-SHA1&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&SignatureVersion=1.0&Timestamp=2018-12-23T12%3A46%3A24Z&Version=2018-12-21
Step 2: Construct StringToSign
Build StringToSign by concatenating the HTTP method, a percent-encoded slash, and the percent-encoded CanonicalizedQueryString:
StringToSign = HTTPMethod + "&" + percentEncode("/") + "&" + percentEncode(CanonicalizedQueryString)Where:
HTTPMethodis the HTTP request method, such asGET.percentEncode("/")always produces%2F.percentEncode(CanonicalizedQueryString)applies the same encoding rules from Step 1 to the entire canonical query string.
Example StringToSign for the DescribeFabricOrganization request:
GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeFabricOrganization%26Format%3DXML%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf%26SignatureVersion%3D1.0%26Timestamp%3D2018-12-23T12%253A46%253A24Z%26Version%3D2018-12-21Step 3: Calculate the signature
Compute the HMAC-SHA1 value of
StringToSign. The signing key is your AccessKey Secret followed by an ampersand (&, ASCII 38):Key = AccessKey Secret + "&"For example, if your AccessKey Secret is
testsecret, the key istestsecret&.Base64-encode the HMAC-SHA1 result. This encoded value is your signature.
Percent-encode the signature per RFC 3986, then append it to the request URL as the
Signatureparameter.
Example
This example signs a DescribeFabricOrganization request using test credentials.
Credentials:
| Field | Value |
|---|---|
| AccessKey ID | testid |
| AccessKey Secret | testsecret |
| Signing key | testsecret& |
Pre-signed URL:
http://baas.aliyuncs.com/?Action=DescribeFabricOrganization&Timestamp=2018-12-23T12:46:24Z&Format=XML&AccessKeyId=testid&SignatureMethod=HMAC-SHA1&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2018-12-21&SignatureVersion=1.0Calculated signature (using key testsecret&):
OLeaidS1JvxuMvnyHOwuJ+uX5qY=Signed URL (with Signature appended):
http://baas.aliyuncs.com/?Action=DescribeFabricOrganization&Timestamp=2018-12-23T12:46:24Z&Format=XML&AccessKeyId=testid&SignatureMethod=HMAC-SHA1&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2018-12-21&SignatureVersion=1.0&Signature=VyBL52idtt+oImX0NZC+2ngk15Q=TheSignaturevalue in the final URL (VyBL52idtt+oImX0NZC+2ngk15Q=) differs from the raw calculated signature (OLeaidS1JvxuMvnyHOwuJ+uX5qY=) because the signature value is percent-encoded before being added to the URL per RFC 3986.