BeyondCodeBootcamp / BeyondCodeBootcamp/passkeys

doc: example generating keypairs with WebCrypto & node:crypto

Aperta
#10 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
2
Fork
1
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Parti dall’esempio JavaScript nell’issue, in particolare da generateKeyPair, generateSecp256k1KeyPair e main, e confronta il suo utilizzo di WebCrypto e node:crypto. Conferma i nomi degli algoritmi documentati e i file generati previsti, inclusa la questione aperta sul supporto di secp256k1 in WebCrypto; il lavoro è completato quando l’esempio copre chiaramente la generazione della coppia di chiavi prevista.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, node.js
Ambito
cryptography, documentation
Tipo di issue
Documentazione
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.