October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Match JSON Bytes to Fix HMAC Verification in TypeScript

HMAC verifies bytes, not JSON objects. Compare the provider’s signing contract with the exact body bytes, key, algorithm, encoding, and signature used in production.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

HMAC verification fails when the verifier receives different key bytes, algorithm settings, signature bytes, or message bytes from those used to create the signature. With JSON, a common debugging trap is parsing a request and serializing it again: the object may look unchanged while its whitespace, escaping, property order, or number representation—and therefore its authenticated bytes—differ.

TypeScript itself is not established as a cause. To find the fault, compare the sender’s signing contract with the exact bytes, decoding steps, and crypto configuration used by the deployed verifier.

As an Amazon Associate I earn from qualifying purchases.

What HMAC verification actually checks

HMAC authenticates bytes using keying material and a configured hash algorithm. The W3C Web Cryptography specification describes HMAC as calculating and verifying hash-based message authentication codes using the SHA hash functions defined in that specification (Web Cryptography specification, HMAC).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Node.js Web Crypto, subtle.verify(algorithm, key, signature, data) resolves to a boolean when verification completes: true means the supplied signature verifies for that key and data; false means it does not (Node.js Web Crypto API). An exception is a different failure mode: investigate API errors or key-import problems separately from a completed verification that returns false.

Why equivalent-looking JSON can fail

HMAC operates on a byte sequence, not on an abstract JavaScript object. If the sender signs the original HTTP body, parsing that body and then serializing the resulting object can alter the authenticated bytes. Differences can include whitespace, escaping, property order, or how numbers are represented.

RFC 8785 explains the underlying requirement: “Cryptographic operations like hashing and signing need the data to be expressed in an invariant format so that the operations are reliably repeatable.” Its JSON Canonicalization Scheme (JCS) defines deterministic serialization, including sorting object properties (RFC 8785, JSON Canonicalization Scheme).

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Use JCS only if the sender’s contract specifies it or both parties have agreed to it. Applying canonicalization only on the receiving side does not repair a mismatch if the sender signed the original body or used a different serialization rule.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Compare the signing contract before changing code

Write down the sender’s requirements and the verifier’s actual inputs side by side. Generic HMAC examples cannot establish the conventions of a particular integration.

  • Algorithm: the precise HMAC hash required by the sender.
  • Key: the exact key bytes, including how any text, hex, or Base64 representation is decoded.
  • Message: whether the signature covers the original request body or a JSON value serialized by an agreed method.
  • Signature format: the required output encoding, such as hex or Base64, and the corresponding decoding procedure.
  • Runtime and API: the key-import and crypto configuration used in the deployed environment.

These are explicit inputs to Web Crypto verification: algorithm, key, signature, and data. Confirm each from the provider’s signing specification and your implementation rather than inferring it from a sample built for another integration.

Trace the exact bytes through the verifier

Preserve the body if the contract signs the body

If the provider signs the raw request body, verify against those original bytes before JSON parsing changes their representation. Capture the body at the point it enters the verifier and compare it with the byte sequence the sender says it signed. Logging only a parsed object is not enough to establish that the original bytes match.

Make serialization an agreed rule if signing JSON values

If both parties sign a JSON value rather than preserving the transmitted body, establish the exact deterministic serialization each side uses. JCS is one defined option, but it must be part of the shared contract; do not silently add it to only one implementation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check character encoding

Strings must be converted into the same bytes on both sides. Node.js uses UTF-8 when a string is passed to a crypto update operation without an explicit encoding; a Buffer is passed as bytes (Node.js crypto API). Inspect the byte representation rather than assuming two visually identical strings have the same encoding.

Decode the signature into bytes

Keep HMAC output as bytes until converting it to the format the integration requires. Decode incoming hex or Base64 according to the sender’s stated format, then supply the resulting bytes to verification. Do not compare a textual encoding directly with raw digest bytes or interpret binary output as Unicode: Node.js cautions that cryptographic outputs are pseudorandom byte sequences, not ordinary text (Node.js crypto API).

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A production debugging sequence

  1. Record the contract. Confirm the HMAC hash, key bytes and decoding, signed-message definition, and signature encoding with the sender’s specification.
  2. Capture verifier inputs. At the verification boundary, inspect the data bytes, decoded signature bytes, selected algorithm, and imported key configuration. Protect secrets: do not expose key material in logs.
  3. Compare raw and rebuilt payloads. Determine whether the sender signs the original body or a serialized JSON value. If the receiver parses and rebuilds the body, check for changes to whitespace, escaping, property order, and numbers.
  4. Verify encoding and decoding. Confirm the character-to-byte encoding for the message and the exact decoding used for the signature representation.
  5. Compare working and failing environments. Check the same seven items in each: signed bytes, original versus reserialized body, canonicalization and property order, text encoding, hash and key bytes, signature encoding, and runtime/API and key-import configuration.
  6. Classify the failure. Treat a returned false as a completed comparison that did not match. Treat a thrown error as an API, algorithm, or key setup problem and diagnose it separately.

What the evidence does—and does not—establish

The standards and API documentation establish that HMAC verification depends on the configured algorithm, key, signature, and message data, and that JSON needs a stable representation when cryptographic operations are expected to be repeatable. They do not identify a universal production root cause or show that TypeScript itself causes failures. The incident-specific answer depends on the provider’s signing specification, a representative payload and signature, the implementation, and runtime details.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.