Updated on 2026-09-21 GMT+08:00

Generating a MAC

Function

This API is used to generate a MAC.

Constraints

  • This API is supported only for keys whose key_usage is GENERATE_VERIFY_MAC.

Calling Method

For details, see Calling APIs.

URI

POST /v1.0/{project_id}/kms/generate-mac

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Definition

Project ID. For details, see Obtaining a Project ID.

Constraints

N/A

Range

The value returned by the IAM API is used, which contains 32 characters.

Default Value

N/A

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

Definition

User token. It can be obtained by calling the IAM API. The value of X-Subject-Token in the response header is the user token.

Constraints

N/A

Range

Obtain the value by calling the IAM API for obtaining the user token.

Default Value

N/A

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

key_id

Yes

String

Definition

Key ID

Constraints

  • The value must be a 36-byte ID.

  • The value must match the regular expression ^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$.

Range

N/A

Default Value

N/A

mac_algorithm

Yes

String

Definition

MAC algorithm

Constraints

HMAC_SM3 is supported only in Chinese mainland.

Range

  • HMAC_SHA_256

  • HMAC_SHA_384

  • HMAC_SHA_512

  • HMAC_SM3

Default Value

N/A

message

Yes

String

Definition

Message to be processed

Constraints

  • The original message must contain 1 to 4,096 characters.

  • The original message needs to be converted to the Base64 format before being imported.

Range

N/A

Default Value

N/A

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

key_id

String

Definition

Key ID

Range

N/A

mac_algorithm

String

Definition

MAC algorithm

Range

  • HMAC_SHA_256

  • HMAC_SHA_384

  • HMAC_SHA_512

  • HMAC_SM3

mac

String

Definition

Generated MAC

Range

N/A

Example Requests

{
  "key_id" : "826314dd-1b5b-4037-b976-5f9b7a17df46",
  "mac_algorithm" : "HMAC_SHA_256",
  "message" : "ZmRzYQ=="
}

Example Responses

Status code: 200

Request succeeded.

{
  "mac_algorithm" : "HMAC_SHA_256",
  "key_id" : "826314dd-1b5b-4037-b976-5f9b7a17df46",
  "mac" : "9d266415acf82985bb44daf4990604f1931384c88fd21ef32b202396755dcfd7"
}

Status Codes

Status Code

Description

200

Request succeeded.

Error Codes

See Error Codes.