CodeSampleX

Sample

jose 6.2.4: generateKeyPair

Verified sample for npm jose 6.2.4: generateKeyPair. The contract ran on node 22 · linux alpine/x64 · docker and passed: generateKeyPair generates RSA key…

sha256:0b8bf9ad97763749b763a64ef8e5844daf463e38b320a7f484bf7ca001595ebd

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 npm

Verification-run environments

Environment Contract Stages Run
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-28

Case

HOW
Goal
verify jose.generateKeyPair in pkg:npm/jose@6.2.4
Packages
Symbols
  • jose.generateKeyPair
Created
2026-08-28T03:24:57Z

Contract

  1. generateKeyPair generates RSA key pair (RS256, RS384, RS512, PS256, PS384, PS512) with configurable modulusLength compatible with SignJWT and jwtVerify
  2. generateKeyPair generates Elliptic Curve key pair (ES256, ES384, ES512) compatible with SignJWT and jwtVerify
  3. generateKeyPair generates Edwards-curve / OKP key pair (EdDSA with Ed25519) compatible with CompactSign and compactVerify
  4. generateKeyPair generates key pair for asymmetric encryption (RSA-OAEP-256, ECDH-ES with P-256 and X25519) compatible with CompactEncrypt and compactDecrypt
  5. generateKeyPair respects the extractable option for privateKey export and returns CryptoKey instances with proper type and algorithm properties
  6. generateKeyPair rejects unsupported, invalid, or symmetric algorithm identifiers with JOSENotSupported (ERR_JOSE_NOT_SUPPORTED)

Files

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • test/contract.mjs

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify jose.generateKeyPair in pkg:npm/jose@6.2.4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/jose@6.2.4
Demonstrate these symbols/APIs:
  - jose.generateKeyPair

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:ea1e31c2c8cca312acb527b7ccc25bcee99a8a154761fe99865ccfa5cdea4fb0","contract":["generateKeyPair generates RSA key pair (RS256, RS384, RS512, PS256, PS384, PS512) with configurable modulusLength compatible with SignJWT and jwtVerify","generateKeyPair generates Elliptic Curve key pair (ES256, ES384, ES512) compatible with SignJWT and jwtVerify","generateKeyPair generates Edwards-curve / OKP key pair (EdDSA with Ed25519) compatible with CompactSign and compactVerify","generateKeyPair generates key pair for asymmetric encryption (RSA-OAEP-256, ECDH-ES with P-256 and X25519) compatible with CompactEncrypt and compactDecrypt","generateKeyPair respects the extractable option for privateKey export and returns CryptoKey instances with proper type and algorithm properties","generateKeyPair rejects unsupported, invalid, or symmetric algorithm identifiers with JOSENotSupported (ERR_JOSE_NOT_SUPPORTED)"],"goal":"verify jose.generateKeyPair in pkg:npm/jose@6.2.4","kind":"HOW","packages":["pkg:npm/jose@6.2.4"],"schemaVersion":1,"symbols":["jose.generateKeyPair"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/jose@6.2.4"],"schemaVersion":1,"subject":"pkg:npm/jose@6.2.4","symbols":["jose.generateKeyPair"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "verify-jose-generate-key-pair",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "verify-jose-generate-key-pair",
      "version": "1.0.0",
      "dependencies": {
        "jose": "6.2.4"
      }
    },
    "node_modules/jose": {
      "version": "6.2.4",
      "resolved": "https://registry.npmjs.org/jose/-/jose-6.2.4.tgz",
      "integrity": "sha512-N8acGzVsQy6M/fjFcxtysNc4Q379TcM5dM/qKkNtsHFji88yANnXTr7BLeP75iPnFwBfQzM/jg2BZ9+HZrHCZA==",
      "license": "MIT",
      "funding": {
        "url": "https://github.com/sponsors/panva"
      }
    }
  }
}
package.json
{
  "name": "verify-jose-generate-key-pair",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "dependencies": {
    "jose": "6.2.4"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify jose.generateKeyPair in pkg:npm/jose@6.2.4",
  "kind": "HOW",
  "packages": [
    "pkg:npm/jose@6.2.4"
  ],
  "symbols": [
    "jose.generateKeyPair"
  ]
}
src/index.mjs
import { generateKeyPair, exportJWK, exportPKCS8, exportSPKI } from 'jose';

/**
 * Generates an asymmetric public/private key pair for a given algorithm.
 *
 * @param {string} alg - JWA asymmetric algorithm identifier (e.g. 'RS256', 'PS256', 'ES256', 'EdDSA', 'ECDH-ES')
 * @param {object} [options] - Key generation options
 * @param {boolean} [options.extractable] - Whether keys are extractable
 * @param {number} [options.modulusLength] - Modulus length for RSA algorithms
 * @param {string} [options.crv] - Named curve for EC/OKP algorithms
 * @returns {Promise<{ publicKey: CryptoKey, privateKey: CryptoKey }>}
 */
export async function createKeyPair(alg, options) {
  return generateKeyPair(alg, options);
}

/**
 * Exports a key to JSON Web Key (JWK) representation.
 *
 * @param {CryptoKey} key - The key to export
 * @returns {Promise<import('jose').JWK>}
 */
export async function exportKeyJWK(key) {
  return exportJWK(key);
}

export { generateKeyPair, exportJWK, exportPKCS8, exportSPKI };
export default generateKeyPair;
test/contract.mjs
import assert from 'node:assert/strict';
import { generateKeyPair, SignJWT, jwtVerify, CompactSign, compactVerify, CompactEncrypt, compactDecrypt, exportJWK, errors } from 'jose';
import { createKeyPair, exportKeyJWK } from '../src/index.mjs';

const encoder = new TextEncoder();
const decoder = new TextDecoder();

// 1. generateKeyPair generates RSA key pair (RS256, RS384, RS512, PS256, PS384, PS512) with configurable modulusLength compatible with SignJWT and jwtVerify
{
  const rsaAlgs = ['RS256', 'RS384', 'RS512', 'PS256', 'PS384', 'PS512'];
  for (const alg of rsaAlgs) {
    const { publicKey, privateKey } = await createKeyPair(alg, { modulusLength: 2048 });
    assert.equal(typeof publicKey, 'object');
    assert.equal(typeof privateKey, 'object');
    assert.equal(publicKey.type, 'public');
    assert.equal(privateKey.type, 'private');

    const expectedName = alg.startsWith('PS') ? 'RSA-PSS' : 'RSASSA-PKCS1-v1_5';
    assert.equal(publicKey.algorithm.name, expectedName);
    assert.equal(publicKey.algorithm.modulusLength, 2048);

    const token = await new SignJWT({ user: 'rsa-test', alg })
      .setProtectedHeader({ alg })
      .sign(privateKey);
    assert.equal(typeof token, 'string');

    const { payload, protectedHeader } = await jwtVerify(token, publicKey);
    assert.equal(payload.user, 'rsa-test');
    assert.equal(payload.alg, alg);
    assert.equal(protectedHeader.alg, alg);
  }
}

// 2. generateKeyPair generates Elliptic Curve key pair (ES256, ES384, ES512) compatible with SignJWT and jwtVerify
{
  const ecConfigs = [
    { alg: 'ES256', namedCurve: 'P-256' },
    { alg: 'ES384', namedCurve: 'P-384' },
    { alg: 'ES512', namedCurve: 'P-521' },
  ];

  for (const { alg, namedCurve } of ecConfigs) {
    const { publicKey, privateKey } = await generateKeyPair(alg);
    assert.equal(publicKey.type, 'public');
    assert.equal(privateKey.type, 'private');
    assert.equal(publicKey.algorithm.name, 'ECDSA');
    assert.equal(publicKey.algorithm.namedCurve, namedCurve);

    const token = await new SignJWT({ user: 'ec-test', namedCurve })
      .setProtectedHeader({ alg })
      .sign(privateKey);

    const { payload, protectedHeader } = await jwtVerify(token, publicKey);
    assert.equal(payload.user, 'ec-test');
    assert.equal(payload.namedCurve, namedCurve);
    assert.equal(protectedHeader.alg, alg);
  }
}

// 3. generateKeyPair generates Edwards-curve / OKP key pair (EdDSA with Ed25519) compatible with CompactSign and compactVerify
{
  const { publicKey, privateKey } = await generateKeyPair('EdDSA', { crv: 'Ed25519' });
  assert.equal(publicKey.type, 'public');
  assert.equal(privateKey.type, 'private');
  assert.equal(publicKey.algorithm.name, 'Ed25519');

  const rawPayload = encoder.encode('eddsa compact signed message');
  const jws = await new CompactSign(rawPayload)
    .setProtectedHeader({ alg: 'EdDSA' })
    .sign(privateKey);

  assert.equal(typeof jws, 'string');
  const parts = jws.split('.');
  assert.equal(parts.length, 3);

  const { payload: verifiedPayload, protectedHeader } = await compactVerify(jws, publicKey);
  assert.equal(decoder.decode(verifiedPayload), 'eddsa compact signed message');
  assert.equal(protectedHeader.alg, 'EdDSA');
}

// 4. generateKeyPair generates key pair for asymmetric encryption (RSA-OAEP-256, ECDH-ES with P-256 and X25519) compatible with CompactEncrypt and compactDecrypt
{
  // RSA-OAEP-256 key pair
  {
    const { publicKey, privateKey } = await generateKeyPair('RSA-OAEP-256', { modulusLength: 2048 });
    assert.equal(publicKey.type, 'public');
    assert.equal(privateKey.type, 'private');
    assert.equal(publicKey.algorithm.name, 'RSA-OAEP');

    const plaintext = encoder.encode('secret rsa-oaep payload');
    const jwe = await new CompactEncrypt(plaintext)
      .setProtectedHeader({ alg: 'RSA-OAEP-256', enc: 'A256GCM' })
      .encrypt(publicKey);

    const { plaintext: decrypted, protectedHeader } = await compactDecrypt(jwe, privateKey);
    assert.equal(decoder.decode(decrypted), 'secret rsa-oaep payload');
    assert.equal(protectedHeader.alg, 'RSA-OAEP-256');
    assert.equal(protectedHeader.enc, 'A256GCM');
  }

  // ECDH-ES with P-256
  {
    const { publicKey, privateKey } = await generateKeyPair('ECDH-ES', { crv: 'P-256' });
    assert.equal(publicKey.type, 'public');
    assert.equal(privateKey.type, 'private');
    assert.equal(publicKey.algorithm.name, 'ECDH');
    assert.equal(publicKey.algorithm.namedCurve, 'P-256');

    const plaintext = encoder.encode('secret ecdh p-256 payload');
    const jwe = await new CompactEncrypt(plaintext)
      .setProtectedHeader({ alg: 'ECDH-ES', enc: 'A128GCM' })
      .encrypt(publicKey);

    const { plaintext: decrypted, protectedHeader } = await compactDecrypt(jwe, privateKey);
    assert.equal(decoder.decode(decrypted), 'secret ecdh p-256 payload');
    assert.equal(protectedHeader.alg, 'ECDH-ES');
  }

  // ECDH-ES with X25519
  {
    const { publicKey, privateKey } = await generateKeyPair('ECDH-ES', { crv: 'X25519' });
    assert.equal(publicKey.type, 'public');
    assert.equal(privateKey.type, 'private');
    assert.equal(publicKey.algorithm.name, 'X25519');

    const plaintext = encoder.encode('secret ecdh x25519 payload');
    const jwe = await new CompactEncrypt(plaintext)
      .setProtectedHeader({ alg: 'ECDH-ES', enc: 'A256GCM' })
      .encrypt(publicKey);

    const { plaintext: decrypted, protectedHeader } = await compactDecrypt(jwe, privateKey);
    assert.equal(decoder.decode(decrypted), 'secret ecdh x25519 payload');
    assert.equal(protectedHeader.alg, 'ECDH-ES');
  }
}

// 5. generateKeyPair respects the extractable option for privateKey export and returns CryptoKey instances with proper type and algorithm properties
{
  const extractablePair = await generateKeyPair('ES256', { extractable: true });
  assert.equal(extractablePair.publicKey.extractable, true);
  assert.equal(extractablePair.privateKey.extractable, true);

  const publicJWK = await exportKeyJWK(extractablePair.publicKey);
  assert.equal(publicJWK.kty, 'EC');
  assert.equal(publicJWK.crv, 'P-256');
  assert.equal(typeof publicJWK.x, 'string');
  assert.equal(typeof publicJWK.y, 'string');
  assert.equal(publicJWK.d, undefined);

  const privateJWK = await exportKeyJWK(extractablePair.privateKey);
  assert.equal(privateJWK.kty, 'EC');
  assert.equal(privateJWK.crv, 'P-256');
  assert.equal(typeof privateJWK.d, 'string');

  const nonExtractablePair = await generateKeyPair('ES256', { extractable: false });
  assert.equal(nonExtractablePair.publicKey.extractable, true);
  assert.equal(nonExtractablePair.privateKey.extractable, false);

  await assert.rejects(
    async () => {
      await exportJWK(nonExtractablePair.privateKey);
    },
    (err) => err instanceof TypeError
  );

  const defaultPair = await generateKeyPair('ES256');
  assert.equal(defaultPair.privateKey.extractable, false);
}

// 6. generateKeyPair rejects unsupported, invalid, or symmetric algorithm identifiers with JOSENotSupported (ERR_JOSE_NOT_SUPPORTED)
{
  await assert.rejects(
    async () => {
      await generateKeyPair('HS256');
    },
    (err) => err instanceof errors.JOSENotSupported && err.code === 'ERR_JOSE_NOT_SUPPORTED'
  );

  await assert.rejects(
    async () => {
      await generateKeyPair('A256GCM');
    },
    (err) => err instanceof errors.JOSENotSupported && err.code === 'ERR_JOSE_NOT_SUPPORTED'
  );

  await assert.rejects(
    async () => {
      await generateKeyPair('UNSUPPORTED_ALG');
    },
    (err) => err instanceof errors.JOSENotSupported && err.code === 'ERR_JOSE_NOT_SUPPORTED'
  );

  await assert.rejects(
    async () => {
      await generateKeyPair();
    },
    (err) => err instanceof errors.JOSENotSupported && err.code === 'ERR_JOSE_NOT_SUPPORTED'
  );
}

console.log('Contract tests passed successfully.');

Origin Seeder

anonymous