Skip to content

Commit

Permalink
Support for fingerprinting ssh keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mcavage committed Sep 14, 2011
1 parent 5242873 commit ddff235
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -18,6 +18,7 @@ module.exports = {
signRequest: signer.signRequest,

sshKeyToPEM: util.sshKeyToPEM,
sshKeyFingerprint: util.fingerprint,

verify: verify.verifySignature,
verifySignature: verify.verifySignature
Expand Down
36 changes: 36 additions & 0 deletions lib/util.js
@@ -1,5 +1,7 @@
// Copyright 2011 Joyent, Inc. All rights reserved.

var crypto = require('crypto');

var asn1 = require('asn1');
var ctype = require('ctype');

Expand Down Expand Up @@ -123,6 +125,40 @@ module.exports = {
newKey += '\n';

return '-----BEGIN PUBLIC KEY-----' + newKey + '-----END PUBLIC KEY-----\n';
},


/**
* Generates an OpenSSH fingerprint from an ssh public key.
*
* @param {String} key an OpenSSH public key.
* @return {String} key fingerprint.
* @throws {TypeError} on bad input.
* @throws {Error} if what you passed doesn't look like an ssh public key.
*/
fingerprint: function(key) {
if (!key || typeof(key) !== 'string')
throw new TypeError('ssh_key (string) required');

var pieces = key.split(' ');
if (!pieces || !pieces.length || pieces.length < 2)
throw new Error('invalid ssh key');

var data = new Buffer(pieces[1], 'base64');

var hash = crypto.createHash('md5');
hash.update(data);
var digest = hash.digest('hex');

var fp = '';
for (var i = 0; i < digest.length; i++) {
if (i && i % 2 === 0)
fp += ':';

fp += digest[i];
}

return fp;
}


Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Joyent, Inc",
"name": "http-signature",
"description": "Reference implementation of Joyent's HTTP Signature Scheme",
"version": "0.9.3",
"version": "0.9.4",
"homepage": "http://www.joyent.com",
"repository": {
"type": "git",
Expand All @@ -18,7 +18,7 @@
},
"dependencies": {
"asn1": "0.1.5",
"ctype": "0.0.3",
"ctype": "0.1.0",
"sprintf": "0.1.1"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions tst/convert.test.js
Expand Up @@ -2,6 +2,7 @@

var test = require('tap').test;

var sshKeyFingerprint = require('../lib/index').sshKeyFingerprint;
var sshKeyToPEM = require('../lib/index').sshKeyToPEM;


Expand Down Expand Up @@ -80,3 +81,10 @@ test('4096b rsa ssh key', function(t) {
t.equal(sshKeyToPEM(SSH_4096), PEM_4096);
t.end();
});


test('fingerprint', function(t) {
var fp = sshKeyFingerprint(SSH_1024);
t.equal(fp, '59:a4:61:0e:38:18:9f:0f:28:58:2a:27:f7:65:c5:87');
t.end();
});

0 comments on commit ddff235

Please sign in to comment.