Generate ECDSA Key Pair
You can find examples below describing how to generate a public and private key pair.
Generate an ECDSA DER key pair on the command line using OpenSSL
1) Generate a new private key and output to file as a PEM.
openssl ecparam -name prime256v1 -genkey -noout -out privkey.pem2) Using your private key PEM, generate a private key DER and output to file.
openssl pkcs8 -topk8 -inform PEM -outform DER -in privkey.pem -out privkey.der -nocrypt3) Using your private key DER, generate a public key DER and output to file.
openssl ec -inform der -in privkey.der -pubout -outform der -out pubkey.der$ cat privkey.der | base64
$ cat pubkey.der | base64Generate an ECDSA key pair with NodeJS
const 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 };
}Generate an ECDSA key pair with Python
Last updated