Created
March 29, 2016 13:50
-
-
Save ekristen/65208645947503ad3b85 to your computer and use it in GitHub Desktop.
Generate a secp521r1 EC keypair
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var child_process = require('child_process') | |
| function generateKeyPair(callback) { | |
| var keypair = { | |
| privKey: '', | |
| pubKey: '' | |
| } | |
| var priv_openssl = child_process.spawn('openssl', ['ecparam', '-name', 'secp521r1', '-genkey', '-noout']) | |
| var pub_openssl = require('child_process').spawn('openssl' ,['ec','-pubout', '-conv_form', 'compressed']) | |
| pub_openssl.stdout.setEncoding('utf8') | |
| pub_openssl.stdout.on('data', function(data) { | |
| keypair.pubKey += data | |
| }) | |
| pub_openssl.stdout.on('end', function() { | |
| var ecFound = /-----BEGIN PUBLIC KEY-----/.exec(keypair.pubKey); | |
| if (!ecFound) { | |
| callback(new Error("Output doesn't look like a EC public key")) | |
| } | |
| return callback(null, keypair) | |
| }) | |
| pub_openssl.on('error', function(err){ | |
| return callback(err) | |
| }) | |
| priv_openssl.stdout.on('data', function(data) { | |
| keypair.privKey += data | |
| }) | |
| priv_openssl.stdout.on('end', function() { | |
| var ecFound = /-----BEGIN EC PRIVATE KEY-----/.exec(keypair.privKey); | |
| if (!ecFound) { | |
| return callback(new Error("Output doesn't look like a EC private key")) | |
| } | |
| }) | |
| priv_openssl.on('error', function(err) { | |
| return callback(err) | |
| }) | |
| pub_openssl.stdin.setEncoding('utf-8') | |
| priv_openssl.stdout.pipe(pub_openssl.stdin) | |
| } | |
| module.exports.generateKeyPair = generateKeyPair |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment