liveBook · Manning

Public key

As explained in API Design Patterns

A public key is the openly distributable component of an asymmetric cryptographic key pair used to verify signatures or perform other public-key operations.

A public key is the shareable half of an asymmetric key pair. A user keeps the private key secret, registers the public key with an API, and lets the server verify signatures made with the private key. section 30.2 section 30.3.1

The pair avoids a shared-secret design: the server receives only the public key, while the private key never leaves the user’s control. A public key can verify a signature but cannot generate the corresponding signature. section 30.3.1 section 30.3.3

Use a public key in an authenticated API workflow by:

  • Generating the key pair on the client, then serializing the public key into a shareable string. section 30.3.1
  • Registering the public key under a user or key identifier so the server can retrieve it later. section 30.2 section 30.3.7
  • Verifying the request digest, recomputed fingerprint, selected public key, and digital signature before authentication. section 30.3.6

Signed HTTP request

A request can identify the public key through keyId="1234" and declare algorithm="rsa-sha256" in its Signature header. The server uses that identifier to select the registered public key, recomputes the request fingerprint, and verifies the signature. section 30.3.5

Listing 30.1 Generating a set of credentials
const crypto = require('crypto');
 
interface KeyPair {
  publicKey: string;
  privateKey: string;
}
 
function generateCredentials(size = 2048): Promise<KeyPair> {
  return new Promise<KeyPair>( (resolve, reject) => {
    crypto.generateKeyPair('rsa', { modulusLength: size },
      (err, pub, priv) => {
      if (err) { return reject(err); }                             #1
      return resolve({
        publicKey: pub.export({ type: 'pkcs1', format: 'pem' }),   #2
        privateKey: priv.export({ type: 'pkcs1', format: 'pem' })
      });
    });
  });
}

Inspect the TypeScript credential-generation listing to see an RSA key pair exported as PEM strings, with the public key ready for distribution. section 30.3.1

How API Design Patterns covers it

How deep it goes
Traces generation, registration, signature verification, request metadata, and digest validation, but defers cryptographic number theory.
Examples
Demonstrates RSA key generation, PEM serialization, signature headers, and a signed PATCH request.
What it assumes
Assumes HTTP request handling and basic credentials, while teaching asymmetric key pairs and signature verification from their security rationale.
What you can do after
Build it from scratch by generating credentials, registering a public key, signing requests, and verifying inbound signatures.
How it connects
Connects asymmetric cryptography with private keys, digital signatures, hashing, request authentication, and HTTP requests.

Also explained in

Compare how 8 books explain public key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.