Create JWK representation of RSA key pair

node v18.11.0
version: master
endpointsharetweet
Create RSA Key pair with DER format
const crypto = require('crypto'); // Generate a new RSA key pair const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'der', }, privateKeyEncoding: { type: 'pkcs8', format: 'der', }, });
Creating JSON Web Keys using generated RSA key pairs. You need to provide unique Key Ids (kid) for your JWKs
// Create JWKs from generated keys const jwkPublicKey = { kty: 'RSA', alg: 'RS256', use: 'sig', kid: 'my-key-id', n: publicKey.toString('base64'), e: 'AQAB', }; const jwkPrivateKey = { kty: 'RSA', alg: 'RS256', use: 'sig', kid: 'my-key-id', n: publicKey.toString('base64'), e: 'AQAB', d: privateKey.toString('base64'), p: '', q: '', dp: '', dq: '', qi: '', };
Geneated JWK representations
console.log('Public JWK:', jwkPublicKey); console.log('Private JWK:', jwkPrivateKey);
Loading…

no comments

    sign in to comment