eckles.js/bin/eckles.js

71 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2018-11-19 05:50:08 +00:00
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var Eckles = require('../index.js');
2018-11-19 05:50:08 +00:00
var infile = process.argv[2];
2018-11-20 05:12:27 +00:00
var format = process.argv[3];
2018-11-19 05:50:08 +00:00
if (!infile) {
infile = 'jwk';
}
if (-1 !== [ 'jwk', 'pem', 'json', 'der', 'sec1', 'pkcs8', 'spki', 'ssh' ].indexOf(infile)) {
console.log("Generating new key...");
Eckles.generate({
format: infile
, namedCurve: format === 'P-384' ? 'P-384' : 'P-256'
, encoding: format === 'der' ? 'der' : 'pem'
}).then(function (key) {
if ('der' === infile || 'der' === format) {
key.private = key.private.toString('binary');
key.public = key.public.toString('binary');
}
console.log(key.private);
console.log(key.public);
}).catch(function (err) {
console.error(err);
process.exit(1);
});
return;
}
2018-11-20 05:12:27 +00:00
var key = fs.readFileSync(infile, 'ascii');
2018-11-19 05:50:08 +00:00
2018-11-20 05:12:27 +00:00
try {
key = JSON.parse(key);
} catch(e) {
// ignore
}
2019-02-07 06:56:13 +00:00
var thumbprint = ('thumbprint' === format);
if (thumbprint) {
format = 'public';
}
2018-11-20 05:12:27 +00:00
if ('string' === typeof key) {
2019-02-07 06:56:13 +00:00
if (thumbprint) {
Eckles.thumbprint({ pem: key }).then(console.log);
return;
}
2018-11-20 16:53:52 +00:00
var pub = (-1 !== [ 'public', 'spki', 'pkix' ].indexOf(format));
Eckles.import({ pem: key, public: (pub || format) }).then(function (jwk) {
2018-11-20 05:12:27 +00:00
console.log(JSON.stringify(jwk, null, 2));
}).catch(function (err) {
console.error(err);
process.exit(1);
});
} else {
2019-02-07 06:56:13 +00:00
if (thumbprint) {
Eckles.thumbprint({ jwk: key }).then(console.log);
return;
}
Eckles.export({ jwk: key, format: format }).then(function (pem) {
2018-11-20 05:12:27 +00:00
console.log(pem);
}).catch(function (err) {
console.error(err);
process.exit(2);
});
}