Permalink
Browse files

format/parse is not the identity

  • Loading branch information...
substack committed Feb 28, 2011
1 parent eac6820 commit 0f6b6d897c4aece53fe1f8e5f9b75f8647fd5db2
Showing with 57 additions and 18 deletions.
  1. +11 −16 key.js
  2. +5 −0 package.json
  3. +41 −2 test/keys.js
View
27 key.js
@@ -18,9 +18,8 @@ function Key (keyData, format) {
}
}
-Key.pack = function (algorithm) {
- var bufs = [].slice.call(arguments, 1)
- .map(function (bigi) { return bigi.toBuffer('mpint') });
+Key.pack = function (algorithm, vars) {
+ var bufs = vars.map(function (bigi) { return bigi.toBuffer('mpint') });
var packed = Buffers(bufs).slice();
return new Key(packed, algorithm);
};
@@ -49,29 +48,24 @@ Key.parse = function (body) {
};
Key.prototype.toString = function (encoding) {
- return this.fields
- .reduce(function (put, x) {
- return put.put(x.toBuffer('mpint'))
- }, Put().word32be(id.length).put(new Buffer(id)))
- .buffer().toString(encoding || 'base64')
- ;
+ return this.data.toString(encoding || 'base64')
};
-Key.prototype.format = function (format, privOrPub, cols) {
+Key.prototype.format = function (format, aux) {
var fmt = (format || 'ssh2').toLowerCase();
if (fmt === 'ssh2') {
- var p = (privOrPub || '').toUpperCase();
+ var p = (aux || '').toUpperCase();
if (p !== 'PRIVATE' && p !== 'PUBLIC') {
throw new Error('Must specify private or public for ssh2');
}
var algo = this.algorithm.toUpperCase();
+ if (algo === 'DSS') algo = 'DSA';
var wrapped = [];
- for (var i = 0; i < this.data.length; i += 64) {
- wrapped.push(this.data.slice(
- i, Math.min(this.data.length, i + 64)
- ));
+ var data = this.data.toString('base64');
+ for (var i = 0; i < data.length; i += 64) {
+ wrapped.push(data.slice(i, i + 64));
}
return [
@@ -83,7 +77,8 @@ Key.prototype.format = function (format, privOrPub, cols) {
else if (fmt === 'openssh') {
var id = this.algorithm;
if (id === 'dsa') id = 'dss';
- return [ 'ssh-' + id, this.data, email || '' ].join(' ') + '\r\n';
+ var data = this.data.toString('base64');
+ return [ 'ssh-' + id, data, aux || '' ].join(' ') + '\r\n';
}
else throw new Error('Unrecognized format ' + format.toString());
};
View
@@ -11,6 +11,11 @@
"lib" : ".",
"test" : "./test"
},
+ "dependencies" : {
+ "bigint" : ">=0.0.9",
+ "buffers" : ">=0.0.1",
+ "put" : ">=0.0.3"
+ },
"scripts" : {
"test" : "expresso"
},
View
@@ -1,9 +1,48 @@
-var assert = require('assert');
var Key = require('keyx/key');
+var dss = require('keyx/dss');
+
+var assert = require('assert');
+var bigint = require('bigint');
+var Buffers = require('buffers');
-exports.create = function () {
+exports.createKey = function () {
assert.eql(
new Key('test', 'dss').data,
new Buffer('test', 'base64')
);
+
+ assert.eql(
+ new Key('test', 'dss-base64').data,
+ new Buffer('test', 'base64')
+ );
+
+ assert.eql(
+ new Key('abcdef', 'dss-hex').data,
+ new Buffer('abcdef', 'hex')
+ );
+};
+
+exports.packParseKey = function () {
+ var pubkey = dss.generate();
+ var vars = [ pubkey.p, pubkey.q, pubkey.g, pubkey.y ];
+ var data = Buffers(vars.map(function (x) {
+ return x.toBuffer('mpint')
+ })).slice();
+
+ var key = Key.pack('dss', vars);
+ assert.eql(key.data, data);
+
+ assert.eql(key.toString(), data.toString('base64'));
+
+ assert.throws(function () {
+ key.format('ssh2');
+ });
+
+ var ssh2 = key.format('ssh2', 'private');
+ var kssh2 = Key.parse(ssh2);
+ assert.eql(key.data, kssh2.data);
+
+ var openssh = key.format('openssh', 'moo@moo.com');
+ var kopenssh = Key.parse(openssh);
+ assert.eql(key.data, kopenssh.data);
};

0 comments on commit 0f6b6d8

Please sign in to comment.