Generating Payload Checksum
- Sort Payload Keys - The keys of the payload are sorted alphabetically to maintain consistency.
- Concatenate Sorted Values - The values of the sorted object are concatenated into a single string.
- Generate HMAC-SHA256 Hash - The concatenated string is hashed using HMAC-SHA256 with the provided secret key.
- Return the Hex Digest - The resulting hash is returned in hexadecimal format.
- The order of values matters, so sorting is crucial to maintain consistency.
- The function assumes all values are strings; if they contain complex types, they should be serialized properly before hashing.
Examples
Validating Payload Checksum
- Extract Checksum - Extract the checksum from the received request.
-
Recompute Checksum - Using the same
createPayloadChecksum
function, recompute the checksum from the received payload using the checksum key. -
Compare the Computed and Received Checksum
- If both checksums match, the payload is valid and untampered.
- If they do not match, reject the request as it may have been modified.