示例
jose 6.2.4: generateKeyPair
已验证示例 — npm jose 6.2.4: generateKeyPair. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: generateKeyPair generates RSA key pair (RS256, RS384, RS512…
sha256:0b8bf9ad97763749b763a64ef8e5844daf463e38b320a7f484bf7ca001595ebd
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
linux 24 · ubuntu · glibc 2.39 x64 npm
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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 |
案例
HOW- 目标
- verify jose.generateKeyPair in pkg:npm/jose@6.2.4
- 符号
-
- jose.generateKeyPair
- 创建时间
- 2026-08-28T03:24:57Z
契约
- 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)
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.mjs
- test/contract.mjs
源代码
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.
{"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"}
{
"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"
}
}
}
}
{
"name": "verify-jose-generate-key-pair",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"jose": "6.2.4"
}
}
{
"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"
]
}
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;
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.');
原始种子者
匿名