generate_mac
Generate Message Authentication Codes (MACs) for payment messages, ensuring integrity and authenticity for ISO 8583 transactions and EMV scripts. Supports CMAC, CBC-MAC, Retail MAC, HMAC, and DUKPT key variants.
Instructions
Call this when implementing or validating MAC generation for ISO 8583 transactions, EMV issuer scripts, or any payment message authentication. Also call to verify which MAC algorithm a given M-key type supports before writing code.
Preferred key type: M6 (CMAC). Legacy: M1 (CBC-MAC), M3 (Retail MAC), M0 (AS2805). ISO 8583 field 64 (primary MAC) or field 128 (secondary MAC).
generation_attributes examples: CMAC: {"Algorithm": "CMAC"} ISO 9797-1 Alg 1 (CBC-MAC): {"Algorithm": "ISO9797_ALGORITHM1"} ISO 9797-1 Alg 3 (Retail): {"Algorithm": "ISO9797_ALGORITHM3"} HMAC-SHA256: {"Algorithm": "HMAC_SHA256"} DUKPT CMAC (AES): {"DukptCmac": {"KeySerialNumber": "...", "DukptKeyVariant": "BIDIRECTIONAL", "DukptDerivationType": "AES_128"}} DUKPT Alg 1 (3DES): {"DukptIso9797Algorithm1": {"KeySerialNumber": "...", "DukptKeyVariant": "REQUEST", "DukptDerivationType": "TDES_2KEY"}} DUKPT Alg 3 (3DES): {"DukptIso9797Algorithm3": {"KeySerialNumber": "...", "DukptKeyVariant": "REQUEST", "DukptDerivationType": "TDES_2KEY"}}
DUKPT key variant rules: BIDIRECTIONAL is only valid for AES DUKPT (DukptDerivationType: AES_128/AES_192/AES_256). For 3DES DUKPT (TDES_2KEY/TDES_3KEY) use REQUEST (terminal→host) or RESPONSE (host→terminal). Passing BIDIRECTIONAL with a TDES derivation type returns ValidationException.
CMAC mac_length note: generate_mac with CMAC always returns the full 16-byte (32H) MAC regardless of the mac_length parameter — APC returns the full value and the caller truncates. If you need verify_mac to accept a truncated CMAC (e.g. 4-byte / 8H from a payShield M8 flow), see verify_mac docstring for the workaround.
Args: key_identifier: ARN or alias of MAC key (M0, M1, M3, M6, or M7) message_data: Hex-encoded message to authenticate generation_attributes: MAC algorithm parameters mac_length: Output MAC length in nibbles/hex-digits (NOT bytes): 8=4-byte MAC, 16=8-byte MAC; omit for algorithm default
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| mac_length | No | ||
| message_data | Yes | ||
| key_identifier | Yes | ||
| generation_attributes | Yes |