liveBook · Manning

Private key

As explained in API Design Patterns

A private key is the confidential component of an asymmetric cryptographic key pair used to generate digital signatures or perform other private-key operations.

A private key is the confidential half of an asymmetric key pair. Keep it secret: possessing it proves control of the identity and allows request signatures that a corresponding public key can verify. section 30.2 section 30.3.1

Use private keys according to these rules:

  • Generate the key pair on the user side; never transmit the private key to the API server. section 30.3.1
  • Sign the exact request payload with the private key, then let the server verify it using the registered public key. section 30.3.3

Signing an API payload

A Node.js client can pass a string payload and its private key to crypto.createSign('rsa-sha256'), producing a signature for the server to verify. section 30.3.3

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' })
      });
    });
  });
}

See a TypeScript example generating an RSA key pair and exporting both keys as PEM strings. section 30.3.1

How API Design Patterns covers it

How deep it goes
Explains generation, secrecy, signing, and verification, while deferring cryptographic mathematics and payload design.
Examples
Uses TypeScript key generation and Node.js crypto examples for signing and verifying arbitrary strings.
What it assumes
Starts from key-pair generation and assumes only familiarity with API requests and standard cryptographic libraries.
What you can do after
Apply it via Node.js crypto to generate credentials and sign API payloads without exposing the private key.
How it connects
Connects private keys with public keys, asymmetric cryptography, digital signatures, request authentication, and nonrepudiation.

Also explained in

Compare how 13 books explain private 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.