Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

doc: example generating keypairs with WebCrypto & node:crypto

Open
#10 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, node.js

Research direction

Start from the JavaScript example in the issue, especially generateKeyPair, generateSecp256k1KeyPair, and main, and compare its WebCrypto and node:crypto usage. Confirm the documented algorithm names and expected generated files, including the open question about secp256k1 support in WebCrypto; done means the example clearly covers the intended keypair generation.

Written by the indexing model from the issue text.

Description

'use strict';

let Fs = require('node:fs/promises');

async function generateKeyPair(algo) {
  let keyPair = await crypto.subtle.generateKey(algo, true, ['sign', 'verify']);

  let name = algo.namedCurve || algo.name;

  let privateKeyAb = await crypto.subtle.exportKey('pkcs8', keyPair.privateKey);
  let privateKeyDER = new Uint8Array(privateKeyAb);
  let privateKeyPath = `./key.${name}.der`.toLowerCase();
  await Fs.writeFile(privateKeyPath, privateKeyDER);

  let publicKeyAb = await crypto.subtle.exportKey('spki', keyPair.publicKey);
  let publicKeyDER = new Uint8Array(publicKeyAb);
  let publicKeyPath = `./pub.${name}.der`.toLowerCase();
  await Fs.writeFile(publicKeyPath, publicKeyDER);

  console.info(`Key pair generated and saved as ${privateKeyPath} and ${publicKeyPath}`);
}

let Crypto = require('node:crypto');

async function generateSecp256k1KeyPair() {
  return new Promise(function (resolve, reject) {
    Crypto.generateKeyPair(
      'ec',
      {
        namedCurve: 'secp256k1',
        publicKeyEncoding: {
          type: 'spki',
          format: 'der',
        },
        privateKeyEncoding: {
          type: 'pkcs8',
          format: 'der',
        },
      },
      async function (err, publicKey, privateKey) {
        if (err) {
          return reject(err);
        }

        await Fs.writeFile('./key.secp256k1.der', privateKey);
        await Fs.writeFile('./pub.secp256k1.der', publicKey);

        console.info(
          'secp256k1 key pair generated and saved as ./key.secp256k1.der and ./pub.secp256k1.der',
        );
      },
    );
  });
}

async function main() {
  await generateKeyPair({ name: 'Ed25519' });
  await generateKeyPair({ name: 'ECDSA', namedCurve: 'P-256' });
  // this should be possible with WebCrypto as well, but I don't know the name to use
  //await generateKeyPair({ name: 'ECDSA', namedCurve: 'secp256k1' });
  // it does work with node:crypto
  await generateSecp256k1KeyPair();
}

main().catch(function (err) {
  console.error(err.stack);
  process.exit(1);
});
Dominant language
JavaScript
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from BeyondCodeBootcamp/passkeys

All issues in BeyondCodeBootcamp/passkeys

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.