---
title: "Sign with TypeScript"
description: "Add PDF-PQS signatures to PDFs in Node.js using @pdf-pqs/core with a local key or AWS KMS."
---

> Documentation Index
> Fetch the complete documentation index at: https://pdf-pqs.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Sign with TypeScript

The TypeScript library (`@pdf-pqs/core`) handles the low-level PDF and CMS details for you: it finds the revision boundary, hashes the covered bytes, builds and signs the ML-DSA CMS payload, and appends it to your PDF as a clean incremental update.

You can sign with a local PEM key file for development and testing, or keep your private key protected inside **AWS KMS** for production workloads.

## Before you start

- **Get an ML-DSA-65 or ML-DSA-87 certificate.** ML-DSA-65 is recommended for end-entity signing keys; ML-DSA-44 is not allowed by the standard.
- **Keep your intermediate certificate chain handy.** The library embeds both your signer certificate and its intermediate chain inside the signature payload so verifiers can build the trust path easily.
- **If using AWS KMS:** create an asymmetric KMS key with key spec `ML_DSA_65` and key usage `SIGN_VERIFY`. Make sure your IAM role or user has `kms:Sign` permission on that key.

## Code examples

> **Version**
>
> The library is version 0.1.0, targeting `draft-laget-pdf-pqs-00`. It runs directly from TypeScript source on Node.js 24.11 or later.

### Install

Clone the repository and install its dependencies:

```sh
git clone https://github.com/pdf-pqs/pdf-pqs-js
cd pdf-pqs-js
pnpm install
```

### Sign and inspect with the CLI

Sign a PDF in Place B using a local key and certificate chain:

```sh
node packages/cli/src/main.ts sign --place b \
  --key test-ca/keys/signer.key.pem --cert test-ca/certs/signer.pem \
  --chain test-ca/certs/client-example.pem --chain test-ca/certs/issuing.pem \
  contract.pdf contract-signed.pdf
```

Use `list` to quickly inspect which PDF-PQS signatures are present in a file (note that `list` only reads metadata and does not verify signatures):

```sh
node packages/cli/src/main.ts list contract-signed.pdf
```

```json title="Output"
[
  {
    "place": "B",
    "name": "pdfpqs-1.p7s",
    "L": 1044,
    "algorithm": "ML-DSA-65",
    "timestamp": null
  }
]
```

### Sign in code with a local key

Pass a PKCS #8 PEM private key, your signer certificate, and your intermediate chain to `LocalSigner.fromPem`. This example uses the public test CA keys from `pdf-pqs/spec`, which are strictly for testing:

```ts title="sign-local.ts"
import { readFile, writeFile } from "node:fs/promises";
import { LocalSigner, signPlaceB } from "@pdf-pqs/core";

const signer = LocalSigner.fromPem(
  await readFile("test-ca/keys/signer.key.pem", "utf8"),
  await readFile("test-ca/certs/signer.pem", "utf8"),
  [
    await readFile("test-ca/certs/client-example.pem", "utf8"),
    await readFile("test-ca/certs/issuing.pem", "utf8"),
  ],
);

const pdf = new Uint8Array(await readFile("contract.pdf"));
await writeFile("contract-signed.pdf", await signPlaceB(pdf, signer));
```

### Sign in production with AWS KMS

In production, keep your ML-DSA private key inside AWS KMS so it never leaves hardware-backed key storage. `AwsKmsSigner` sends the DER-encoded signed attributes to KMS with `MessageType: "RAW"` and `SigningAlgorithm: "ML_DSA_SHAKE_256"` (which produces the pure ML-DSA signature required by PDF-PQS) and automatically detects ML-DSA-65 vs. ML-DSA-87 from your certificate. Install `@aws-sdk/client-kms` alongside the library:

```ts title="sign-kms.ts"
import { readFile, writeFile } from "node:fs/promises";
import { signPlaceB } from "@pdf-pqs/core";
import { AwsKmsSigner } from "@pdf-pqs/core/aws-kms";

const signer = new AwsKmsSigner({
  keyId: "alias/pdfpqs-signer",
  certificate: await readFile("signer.pem", "utf8"),
  chain: [await readFile("issuing-ca.pem", "utf8")],
});

const pdf = new Uint8Array(await readFile("contract.pdf"));
await writeFile("contract-signed.pdf", await signPlaceB(pdf, signer));
```

The CLI supports the exact same KMS flow: pass `--kms alias/pdfpqs-signer` instead of `--key` (it uses your standard AWS credentials and region).

For reference, here is the equivalent low-level AWS CLI call that `AwsKmsSigner` makes under the hood:

```sh
aws kms sign \
  --key-id alias/pdfpqs-signer \
  --message-type RAW \
  --signing-algorithm ML_DSA_SHAKE_256 \
  --message fileb://signed-attrs.der \
  --query Signature --output text | base64 --decode > signature.bin
```

### Sign in place A (inside a classical signature)

If you want recipients to see a standard classical signature in Adobe Acrobat alongside the post-quantum signature, use `signPlaceA`. It applies an RSA or ECDSA host signature and embeds the PDF-PQS signature inside it as an unsigned attribute. Continuing from `sign-local.ts`:

```ts title="sign-place-a.ts"
import { hostSignerFromPem, signPlaceA } from "@pdf-pqs/core";

const host = hostSignerFromPem(
  await readFile("test-ca/keys/classical-seal.key.pem", "utf8"),
  await readFile("test-ca/certs/classical-seal.pem", "utf8"),
  [await readFile("test-ca/certs/classical-root.pem", "utf8")],
);
await writeFile("contract-sealed.pdf", await signPlaceA(pdf, signer, host));
```

### Adding timestamps

Both `signPlaceB` and `signPlaceA` accept an optional `tsa` parameter for attaching an RFC 3161 signature timestamp. The library includes `LocalTimestampAuthority` for testing with a local key (it does not bundle an HTTP TSA client). Remember that the timestamp authority must sign with ML-DSA-65 or ML-DSA-87, because verifiers ignore classical timestamps.

## What happens under the hood

When you call `signPlaceA` or `signPlaceB`, the library performs six steps automatically:

1. **Locates `L`** by reading the PDF and finding the exact byte offset at the end of the latest revision.
2. **Hashes `[0, L)`** using SHA-512 to create a tamper-evident fingerprint of the document bytes.
3. **Builds the required CMS signed attributes:** `content-type`, `message-digest`, `signing-certificate-v2`, and `pdf-byte-length` (`L`).
4. **Sorts the signed attributes by DER encoding.** This canonical ordering step is critical because tools like OpenSSL re-encode the attribute set during verification; unsorted attributes would fail validation.
5. **Signs the DER-encoded attributes** with pure ML-DSA and an empty context string (either locally or via AWS KMS).
6. **Appends the signature** in a new incremental update after byte `L`, using Place A (inside a classical host signature) or Place B (as an attached `.p7s` file).

Once you have generated a signed PDF, check it with the [CLI](https://pdf-pqs.org/guides/verify-cli) or inspect the raw payload with [OpenSSL](https://pdf-pqs.org/guides/openssl).

Source: https://pdf-pqs.org/guides/sign-typescript/index.mdx
