Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
1d5f58ff10 |
36
lib/generate-privkey-node-v6.js
Normal file
36
lib/generate-privkey-node-v6.js
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright 2016-2018 AJ ONeal. All rights reserved
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
'use strict';
|
||||
|
||||
var curves = {
|
||||
"P-256": "prime256v1" // 65
|
||||
, "secp256r1": "prime256v1"
|
||||
, "P-384": "secp384r1" // 97
|
||||
, "P-521": "secp521r1" // 133
|
||||
};
|
||||
var crv = "P-256";
|
||||
var ecdh = require('crypto').createECDH(curves[crv] || crv);
|
||||
var keys = ecdh.generateKeys(null, 'uncompressed');
|
||||
console.log(keys.length);
|
||||
console.log(keys.toString('hex'));
|
||||
keys = keys.slice(1);
|
||||
var x = keys.slice(0, keys.byteLength / 2);
|
||||
var y = keys.slice(keys.byteLength / 2);
|
||||
while (0 === x[0]) { x = x.slice(1); }
|
||||
while (0 === y[0]) { y = y.slice(1); }
|
||||
console.log({
|
||||
kty: "EC"
|
||||
, crv: "P-XXX"
|
||||
, x: _toUrlBase64(x)
|
||||
, y: _toUrlBase64(y)
|
||||
});
|
||||
|
||||
function _toUrlBase64(buf) {
|
||||
return buf.toString('base64')
|
||||
.replace(/\+/g, "-")
|
||||
.replace(/\//g, "_")
|
||||
.replace(/=/g,"")
|
||||
;
|
||||
}
|
22
lib/generate-privkey-node.js
Normal file
22
lib/generate-privkey-node.js
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright 2016-2018 AJ ONeal. All rights reserved
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
'use strict';
|
||||
|
||||
module.exports = function (crv) {
|
||||
var keypair = require('crypto').generateKeyPairSync(
|
||||
'ec'
|
||||
, { namedCurve: crv
|
||||
, privateKeyEncoding: { type: 'sec1', format: 'pem' }
|
||||
, publicKeyEncoding: { type: 'spki', format: 'pem' }
|
||||
}
|
||||
);
|
||||
var result = { privateKeyPem: keypair.privateKey.trim() };
|
||||
return result;
|
||||
};
|
||||
|
||||
if (require.main === module) {
|
||||
var keypair = module.exports('P-256');
|
||||
console.info(keypair.privateKeyPem);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user