CmdID

CmdID / aws / aws kms

aws kms generate-mac

Generates a hash-based message authentication code (HMAC) for a message using an HMAC KMS key and a MAC algorithm that the key supports. AWS CLI reference

2 examples 5 options Its page in the AWS CLI reference

Opens CmdID with your command. It is read in your browser and sent nowhere.

Examples

from aws-cli, each command read by CmdID

Example 1: To generate an HMAC for a message

The following generate-mac example generates an HMAC for a message, an HMAC KMS key, and a MAC algorithm. The algorithm must be supported by the specified HMAC KMS key.

In AWS CLI v2, the value of the message parameter must be Base64-encoded. Or, you can save the message in a file and use the fileb:// prefix, which tells the AWS CLI to read binary data from the file.

Before running this command, replace the example key ID with a valid key ID from your AWS account. The key ID must represent a HMAC KMS key with a key usage of GENERATE_VERIFY_MAC.

msg=(echo 'Hello World' | base64)

aws kms generate-mac \
    --key-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \
    --message fileb://Message \
    --mac-algorithm HMAC_SHA_384
Its output
{
    "KeyId": "arn:aws:kms:us-west-2:123456789012:key/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
    "Mac": "<HMAC_TAG>",
    "MacAlgorithm": "HMAC_SHA_384"
}

For more information about using HMAC KMS keys in AWS KMS, see HMAC keys in AWS KMS in the AWS Key Management Service Developer Guide.

Example 2: To save an HMAC in a file (Linux and macOs)

The following generate-mac example generates an HMAC for a short message stored in a local file. The command also gets the Mac property from the response, Base64-decodes it and saves it in the ExampleMac file. You can use the MAC file in a verify-mac command that verifies the MAC.

The generate-mac command requires a Base64-encoded message and a MAC algorithm that your HMAC KMS key supports. To get the MAC algorithms that your KMS key supports, use the describe-key command.

Before running this command, replace the example key ID with a valid key ID from your AWS account. The key ID must represent an asymmetric KMS key with a key usage of GENERATE_VERIFY_MAC.

First, Base64-encode the message and save it in the EncodedMessage file.

echo 'hello world' | base64 > EncodedMessage

Then, generate the HMAC for the encoded message.

aws kms generate-mac --key-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 --message fileb://EncodedMessage --mac-algorithm HMAC_SHA_384 --output text --query Mac | base64 --decode > ExampleMacIdentify
--key-idThe HMAC KMS key to use in the operation.
--messageThe message to be hashed.
--mac-algorithmThe MAC algorithm used in the operation.
--outputThe formatting style for command output.
--queryA JMESPath query to use in filtering the response data.

Trips 1 Sigma rule: lowDecode Base64 Encoded Text by Daniil Yugoslavskiy, oscd.community

This command produces no output. This example extracts the Mac property of the output and saves it in a file.

For more information about using HMAC KMS keys in AWS KMS, see HMAC keys in AWS KMS in the AWS Key Management Service Developer Guide.

Options

5 in the AWS CLI reference
--dry-runChecks if your request will succeed.
--grant-tokensA list of grant tokens.
--key-idThe HMAC KMS key to use in the operation.
--mac-algorithmThe MAC algorithm used in the operation.
--messageThe message to be hashed.