v1.2.1: fingerprint privkeys, doc priv parsing, update bin

This commit is contained in:
AJ ONeal 2018-12-09 01:43:41 -07:00
parent 29802c1af8
commit 7a1d0bbe76
4 changed files with 27 additions and 7 deletions

View File

@ -52,6 +52,10 @@ npm install -g ssh-to-jwk
ssh-to-jwk ~/.ssh/id_rsa.pub ssh-to-jwk ~/.ssh/id_rsa.pub
``` ```
```bash
ssh-to-jwk ~/.ssh/id_rsa
```
# Usage # Usage
You can also use it from JavaScript: You can also use it from JavaScript:
@ -61,10 +65,13 @@ You can also use it from JavaScript:
```js ```js
var fs = require('fs'); var fs = require('fs');
var sshtojwk = require('ssh-to-jwk'); var sshtojwk = require('ssh-to-jwk');
var ssh;
var pub = fs.readFileSync("./id_rsa.pub"); ssh = sshtojwk.parse({ pub: fs.readFileSync("./id_rsa.pub") });
var ssh = sshtojwk.parse({ pub: pub }); console.info(ssh.jwk);
// For OpenSSH PEMs only, use Rasha for standard RSA or Eckles for standard EC
ssh = sshtojwk.parse({ pem: fs.readFileSync("./id_rsa") });
console.info(ssh.jwk); console.info(ssh.jwk);
``` ```

View File

@ -6,17 +6,27 @@ var path = require('path');
var sshtojwk = require('../index.js'); var sshtojwk = require('../index.js');
var pubfile = process.argv[2]; var pubfile = process.argv[2];
var pub = process.argv[3];
if (!pubfile) { if (!pubfile) {
pubfile = path.join(require('os').homedir(), '.ssh/id_rsa.pub'); pubfile = path.join(require('os').homedir(), '.ssh/id_rsa.pub');
} }
var buf = fs.readFileSync(pubfile); var buf = fs.readFileSync(pubfile);
var pub = buf.toString('ascii'); var txt = buf.toString('ascii');
var ssh = sshtojwk.parse({ pub: pub }); var opts = { public: 'public' === pub };
var ssh;
if ('-' === txt[0]) {
opts.pem = txt;
} else {
opts.pub = txt;
}
ssh = sshtojwk.parse(opts);
// Finally! https://superuser.com/a/714195 // Finally! https://superuser.com/a/714195
sshtojwk.fingerprint({ pub: pub }).then(function (fingerprint) { sshtojwk.fingerprint(ssh).then(function (fingerprint) {
console.warn('The key fingerprint is:\n' + fingerprint + ' ' + ssh.comment); console.warn('The key fingerprint is:\n' + fingerprint + ' ' + ssh.comment);
console.info(JSON.stringify(ssh.jwk, null, 2)); console.info(JSON.stringify(ssh.jwk, null, 2));
}); });

View File

@ -5,7 +5,7 @@ var Enc = require('./encoding.js');
var PEM = require('./pem.js'); var PEM = require('./pem.js');
SSH.parse = function (opts) { SSH.parse = function (opts) {
var pub = opts.pub || opts; var pub = opts.pem || opts.pub || opts;
var ssh = SSH.parseBlock(pub); var ssh = SSH.parseBlock(pub);
if ('OPENSSH PRIVATE KEY' === ssh.type) { if ('OPENSSH PRIVATE KEY' === ssh.type) {
ssh = SSH.parsePrivateElements(ssh); ssh = SSH.parsePrivateElements(ssh);
@ -57,6 +57,7 @@ SSH.parsePrivateElements = function (ssh) {
var index = 0; var index = 0;
var padlen = 0; var padlen = 0;
var len; var len;
var pub;
// The last byte will be either // The last byte will be either
// * a non-printable pad character // * a non-printable pad character
@ -91,6 +92,7 @@ SSH.parsePrivateElements = function (ssh) {
len = dv.getUint32(index, false); len = dv.getUint32(index, false);
// throw away public key (it's in the private key) // throw away public key (it's in the private key)
index += 4 + len; index += 4 + len;
pub = ssh.bytes.slice(index - len, index);
// length of dummy checksum + private key + padding // length of dummy checksum + private key + padding
len = dv.getUint32(index, false) - padlen; len = dv.getUint32(index, false) - padlen;
@ -105,6 +107,7 @@ SSH.parsePrivateElements = function (ssh) {
// comment will exist, even if it's an empty string // comment will exist, even if it's an empty string
ssh.comment = Enc.bufToBin(ssh.elements.pop()); ssh.comment = Enc.bufToBin(ssh.elements.pop());
ssh.bytes = pub;
return ssh; return ssh;
}; };
SSH.parseElements = function (buf) { SSH.parseElements = function (buf) {

View File

@ -1,6 +1,6 @@
{ {
"name": "ssh-to-jwk", "name": "ssh-to-jwk",
"version": "1.2.0", "version": "1.2.1",
"description": "💯 SSH to JWK in a lightweight, zero-dependency library.", "description": "💯 SSH to JWK in a lightweight, zero-dependency library.",
"homepage": "https://git.coolaj86.com/coolaj86/ssh-to-jwk.js", "homepage": "https://git.coolaj86.com/coolaj86/ssh-to-jwk.js",
"main": "index.js", "main": "index.js",