ECDSA Signature Verification: A Practical Guide for App Developers
Verify ECDSA signatures in Node.js, understand DER versus raw formats, and separate signature checks from authorization and replay protection.
By AppCradle · Published

To verify an ECDSA signature, you need a trusted public key, the exact bytes that were signed, the agreed curve and hash, and the correct signature encoding. Use a maintained cryptographic library for that check. Then apply the protocol's identity, freshness and authorization rules before changing application state.
For app teams, this distinction matters when processing signed subscription notifications, validating tokens or accepting instructions from another service. A signature can be valid while the event is old, duplicated or intended for a different application. This guide provides a runnable Node.js example and a review process for those boundaries; it does not introduce an AppCradle verification service.
What ECDSA verifies
ECDSA stands for Elliptic Curve Digital Signature Algorithm. A signer uses a private key; a verifier uses the corresponding public key to check the message and signature. Digital signatures support integrity and authentication of the signatory, provided the verifier has established whose key it is. They do not encrypt the message. NIST's Digital Signature Standard defines ECDSA alongside other signature algorithms.
Treat public-key provenance as part of the integration. An attacker can generate a key and a valid signature for their own message. Accepting both from an untrusted request does not establish that your payment provider sent it.
Before implementing a receiver, write down the provider's contract: how keys become trusted, what data is signed, which algorithms are accepted, and how delivery retries work. Keep that contract beside your test fixtures so a future SDK upgrade can be reviewed against it.
Match the algorithm and signature format
For JSON Web Signatures, ES256 means ECDSA with P-256 and SHA-256. ES384 uses P-384 and SHA-384; ES512 uses P-521 and SHA-512. The name is an algorithm contract, not a suggestion to try whichever curve is available. RFC 7518, section 3.4.
Two encodings commonly appear at integration boundaries:
| Encoding | Representation | Practical check |
|---|---|---|
| DER | An ASN.1 structure containing the two signature integers | Configure the verifier for DER; do not assume a fixed byte count |
| Raw / IEEE-P1363 | The fixed-width integers concatenated as `r |
Node.js exposes this choice as dsaEncoding, with der as the default and ieee-p1363 for concatenated integers. Node.js crypto documentation.
An ES256 JWS signature is 64 bytes after base64url decoding: 32 bytes for each integer. That rule is specific to this algorithm and representation, not every ECDSA signature. Do not convert DER to raw by removing a fixed prefix; use a library that understands the structure. JWS ECDSA encoding requirements.
Run a small verification example
Save the following as ecdsa-demo.mjs and run node ecdsa-demo.mjs with a supported Node.js release. It generates disposable local keys, signs a synthetic message and checks several failure cases. No dependencies, credentials or network access are needed.
import assert from "node:assert/strict";
import { generateKeyPairSync, sign, verify } from "node:crypto";
const { publicKey, privateKey } = generateKeyPairSync("ec", {
namedCurve: "prime256v1", // P-256
});
const { publicKey: unrelatedKey } = generateKeyPairSync("ec", {
namedCurve: "prime256v1",
});
const message = Buffer.from('{"event":"demo","sequence":1}', "utf8");
const signature = sign("sha256", message, {
key: privateKey,
dsaEncoding: "ieee-p1363",
});
// A narrow demonstration verifier: trusted P-256 key, raw signature.
function verifyRawP256(bytes, signatureBytes, trustedKey) {
if (!Buffer.isBuffer(bytes) || !Buffer.isBuffer(signatureBytes)) {
return false;
}
if (signatureBytes.length !== 64) return false;
try {
return verify("sha256", bytes, {
key: trustedKey,
dsaEncoding: "ieee-p1363",
}, signatureBytes);
} catch {
return false;
}
}
assert.equal(verifyRawP256(message, signature, publicKey), true);
assert.equal(verifyRawP256(message, signature, unrelatedKey), false);
assert.equal(verifyRawP256(message, signature.subarray(1), publicKey), false);
const changedMessage = Buffer.from(message);
changedMessage[0] ^= 1;
assert.equal(verifyRawP256(changedMessage, signature, publicKey), false);
const changedSignature = Buffer.from(signature);
changedSignature[0] ^= 1;
assert.equal(verifyRawP256(message, changedSignature, publicKey), false);
const derSignature = sign("sha256", message, {
key: privateKey,
dsaEncoding: "der",
});
assert.equal(verifyRawP256(message, derSignature, publicKey), false);
assert.equal(verify("sha256", message, {
key: publicKey,
dsaEncoding: "der",
}, derSignature), true);
console.log("All ECDSA verification checks passed.");
The example uses Node's built-in signing and verification APIs and explicitly chooses the signature representation. prime256v1 is the curve name used here for P-256. The verifier assumes a trusted, correctly configured key; it is not a complete JWT or certificate verifier. Key generation and signature APIs.
In a real integration, load trusted verification keys from the provider's documented trust mechanism. Validate key configuration at startup and keep operational errors observable without logging secrets or complete customer payloads. The example's catch returns failure to demonstrate rejection; production monitoring should distinguish invalid input from a broken verifier configuration. Keep signing private keys out of client bundles and source control.
Preserve the exact signed bytes
For a protocol that signs an HTTP body, retain the bytes required by that protocol before parsing or transforming them. Parsing JSON and serializing it again can change whitespace or property order. Your application may see the same fields while the verifier receives different bytes.
Compact JWS has a different boundary. Its usual signing input is the base64url-encoded protected header, a period, and the base64url-encoded payload. Verifying only the decoded payload misses the protected header and changes the input. Use a JOSE library or the provider's SDK to handle the specified serialization and supported extensions. RFC 7515, JWS overview.
Build a fixture from a documented provider test event. Record the expected encoding and verification result, then test a one-byte change. Avoid “repairing” failed requests by repeatedly switching encodings or trying alternate algorithms until something passes. A failure should lead to inspection of the contract, not a broader acceptance policy.
Validate tokens beyond the signature
Configure an explicit algorithm allowlist. Bind keys to the intended issuer and algorithm; do not let an untrusted header choose an arbitrary verification method or key URL. For JWTs, enforce the expected issuer, audience, token purpose and applicable time claims. Different token types may require different validation rules even when they use the same key. JWT Best Current Practices.
Next, ask what the verified statement authorizes. A token for one workspace should not authorize another. A signed request to prepare a report should not permit a refund. Recheck the current resource and operator permissions where the action executes. Our AI agent permissions guide applies this distinction to tools that can take actions on a user's behalf.
For event processing, design a durable deduplication boundary around the provider's documented event identity and your own business operation. Do not use the signature bytes as the business identifier. Test simultaneous deliveries and a crash between accepting an event and completing its effect. The desired outcome is one recorded operation with recoverable state, rather than two grants of access.
Apply the boundary to App Store notifications
Apple's App Store Server Library provides SignedDataVerifier and verifyAndDecodeNotification for signed notification payloads. Its setup uses trusted Apple root certificates and the expected environment and bundle ID; the production configuration also requires the app's Apple ID. Follow the library's current verification setup, including its certificate-checking options. Apple's verification example.
Use that verifier for the notification's signedPayload instead of applying the demonstration function to the outer HTTP JSON. The SDK handles provider-specific verification that the small example intentionally omits. Where a notification contains nested signed transaction or renewal information, use the library's corresponding verification methods before relying on those contents. SignedDataVerifier methods.
After verification, route the event through your subscription state machine. Define how retries, out-of-order events and reconciliation affect access. Keep a trusted event separate from a financial report: notification processing, entitlement state and revenue totals answer different questions. Our RevenueCat reporting setup guide covers connecting subscription reporting, while the RevenueCat to Google Sheets article discusses a separate export workflow. Each provider's authentication contract still applies.
Troubleshoot verification failures systematically
| Symptom | Inspect first | Useful regression case |
|---|---|---|
| Every event fails | Trusted key, curve, hash and environment | A provider fixture with a known successful result |
| Local signing passes, provider data fails | DER versus raw, and the signed input definition | Correct bytes with deliberately wrong encoding |
| Verification fails after middleware changes | Body parsing, Unicode and serialization | The original bytes versus reserialized JSON |
| Only newly issued tokens fail | Documented key rotation and bounded key refresh | New and retiring trusted keys |
| A valid token reaches the wrong resource | Audience, issuer, purpose and authorization | Valid signature for a different application |
| A valid event causes duplicate effects | Durable event and operation deduplication | Concurrent duplicate delivery and crash recovery |
For a failed request, record a controlled failure category and the minimum diagnostic metadata your service needs. Keep raw tokens, customer data and private key material out of product analytics. Use synthetic fixtures when sharing a reproduction.
Before release, exercise the whole path: trusted key selection, cryptographic rejection, protocol validation, authorization, durable acceptance and recovery. The code example checks the signature boundary. Your integration tests must also show that an authentic but duplicate, expired or misdirected event cannot create an unintended effect.
See the bigger picture.
Bring your store reports together and spend more time understanding what they mean.
Try for free