Generate ECDSA Key Pair
openssl ecparam -name prime256v1 -genkey -noout -out privkey.pemopenssl pkcs8 -topk8 -inform PEM -outform DER -in privkey.pem -out privkey.der -nocryptopenssl ec -inform der -in privkey.der -pubout -outform der -out pubkey.der$ cat privkey.der | base64
$ cat pubkey.der | base64const buffer = require('buffer');
const crypto = require('crypto');
async function createEcdsaKeyPair() {
const keyPair = await crypto.subtle.generateKey(
{
name: 'ECDSA',
namedCurve: 'P-256',
},
true,
['sign', 'verify'],
);
const publicKey = await crypto.subtle.exportKey('spki', keyPair.publicKey)
.then((key) => {
const publicKeyDer = String.fromCharCode
.apply(null, new Uint8Array(key));
const publicKey = Buffer.from(publicKeyDer, 'binary')
.toString('base64');
return publicKey;
}
);
const privateKey = await crypto.subtle.exportKey('pkcs8', keyPair.privateKey)
.then((key) => {
const privateKeyDer = String.fromCharCode
.apply(null, new Uint8Array(key));
const privateKey = Buffer.from(privateKeyDer, 'binary')
.toString('base64');
return privateKey;
}
);
return { publicKey, privateKey };
}Last updated