You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'use strict';letFs=require('node:fs/promises');asyncfunctiongenerateKeyPair(algo){letkeyPair=awaitcrypto.subtle.generateKey(algo,true,['sign','verify']);letname=algo.namedCurve||algo.name;letprivateKeyAb=awaitcrypto.subtle.exportKey('pkcs8',keyPair.privateKey);letprivateKeyDER=newUint8Array(privateKeyAb);letprivateKeyPath=`./key.${name}.der`.toLowerCase();awaitFs.writeFile(privateKeyPath,privateKeyDER);letpublicKeyAb=awaitcrypto.subtle.exportKey('spki',keyPair.publicKey);letpublicKeyDER=newUint8Array(publicKeyAb);letpublicKeyPath=`./pub.${name}.der`.toLowerCase();awaitFs.writeFile(publicKeyPath,publicKeyDER);console.info(`Key pair generated and saved as ${privateKeyPath} and ${publicKeyPath}`);}letCrypto=require('node:crypto');asyncfunctiongenerateSecp256k1KeyPair(){returnnewPromise(function(resolve,reject){Crypto.generateKeyPair('ec',{namedCurve: 'secp256k1',publicKeyEncoding: {type: 'spki',format: 'der',},privateKeyEncoding: {type: 'pkcs8',format: 'der',},},asyncfunction(err,publicKey,privateKey){if(err){returnreject(err);}awaitFs.writeFile('./key.secp256k1.der',privateKey);awaitFs.writeFile('./pub.secp256k1.der',publicKey);console.info('secp256k1 key pair generated and saved as ./key.secp256k1.der and ./pub.secp256k1.der',);},);});}asyncfunctionmain(){awaitgenerateKeyPair({name: 'Ed25519'});awaitgenerateKeyPair({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:cryptoawaitgenerateSecp256k1KeyPair();}main().catch(function(err){console.error(err.stack);process.exit(1);});
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: