Sign signatures

Updated at:

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.

TermDescription
SignatureMethodThe hash algorithm used to calculate the signature. BaaS supports HMAC-SHA1.
SignatureVersionThe signature algorithm version. The current version is 1.0.
SignatureNonceA unique random number included in each request to prevent replay attacks. Use a different value for every request. Universally unique identifiers (UUIDs) are recommended.
SignatureThe Base64-encoded HMAC-SHA1 value calculated from your AccessKey Secret and the canonical request string.
StringToSignAn intermediate string constructed from the HTTP method and the canonicalized query string, used as input to the HMAC-SHA1 algorithm.
percentEncodeA URL-encoding function that encodes characters according to the rules in this document. It differs slightly from standard application/x-www-form-urlencoded encoding.
CanonicalizedQueryStringThe 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-4e0ad82fd6cf

The signing process has two stages:

  1. Construct `StringToSign` — sort and encode all request parameters into a canonical string.

  2. Calculate the signature — run HMAC-SHA1 on StringToSign using 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

  1. Collect all request parameters except Signature. This includes common parameters such as SignatureMethod, SignatureVersion, SignatureNonce, Timestamp, AccessKeyId, and any API-specific parameters.

  2. 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.0

    After 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-21
  3. Percent-encode each parameter name and value using the rules below. Java reference implementation:

    This encoding differs from application/x-www-form-urlencoded (used by java.net.URLEncoder). If you use URLEncoder, apply these replacements afterward: + → %20, * → %2A, %7E → ~.

    Encoding rules:

    Character setEncoding
    A–Z, a–z, 0–9, -, _, ., ~No encoding — pass through as-is
    Space%20 (not +)
    All other characters%XY where XY is 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;
    }
  4. Join each encoded name–value pair with =, then concatenate all pairs with & to form the CanonicalizedQueryString. 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:

  • HTTPMethod is the HTTP request method, such as GET.

  • 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-21

Step 3: Calculate the signature

  1. 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 is testsecret&.

  2. Base64-encode the HMAC-SHA1 result. This encoded value is your signature.

  3. Percent-encode the signature per RFC 3986, then append it to the request URL as the Signature parameter.

Example

This example signs a DescribeFabricOrganization request using test credentials.

Credentials:

FieldValue
AccessKey IDtestid
AccessKey Secrettestsecret
Signing keytestsecret&

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.0

Calculated 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=
The Signature value 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.