Skip to content

Commit ea68a7f

Browse files
committed
Exporting OpenSSH public key
1 parent c3e79f7 commit ea68a7f

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/formats/openssh.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = {
5151
//keynum
5252
reader.off += 4;
5353

54-
//sshpublengtn
54+
//sshpublength
5555
reader.off += 4;
5656

5757
//keytype
@@ -88,10 +88,29 @@ module.exports = {
8888
dq.toBuffer(), // exponent2 -- d mod (q-1)
8989
coeff // coefficient -- (inverse of q) mod p
9090
);
91+
92+
key.sshcomment = readOpenSSHKeyString(reader).toString('ascii');
9193
},
9294

9395
publicExport: function (key, options) {
94-
throw Error('Not implemented yet.');
96+
let ebuf = Buffer.alloc(4)
97+
ebuf.writeUInt32BE(key.e, 0);
98+
//Slice leading zeroes
99+
while(ebuf[0] === 0) ebuf = ebuf.slice(1);
100+
const nbuf = key.n.toBuffer();
101+
const buf = Buffer.alloc(
102+
ebuf.byteLength + 4 +
103+
nbuf.byteLength + 4 +
104+
'ssh-rsa'.length + 4
105+
);
106+
107+
const writer = {buf: buf, off: 0};
108+
writeOpenSSHKeyString(writer, Buffer.from('ssh-rsa'));
109+
writeOpenSSHKeyString(writer, ebuf);
110+
writeOpenSSHKeyString(writer, nbuf);
111+
112+
let comment = key.sshcomment || '';
113+
return 'ssh-rsa ' + buf.toString('base64') + ' ' + comment;
95114
},
96115

97116
publicImport: function (key, data, options) {
@@ -161,4 +180,10 @@ function readOpenSSHKeyString(reader) {
161180
const res = reader.buf.slice(reader.off, reader.off + len);
162181
reader.off += len;
163182
return res;
183+
}
184+
185+
function writeOpenSSHKeyString(writer, data) {
186+
writer.buf.writeInt32BE(data.byteLength, writer.off);
187+
writer.off += 4;
188+
writer.off += data.copy(writer.buf, writer.off);
164189
}

0 commit comments

Comments
 (0)