Sha256: 03945c13c6908e7579667e158764d461426c7c89c7fc216d052aa47fdd3565bd

Contents?: true

Size: 1.85 KB

Versions: 27

Compression:

Stored size: 1.85 KB

Contents

// Copyright 2015 Joyent, Inc.

module.exports = {
	read: read,
	write: write
};

var assert = require('assert-plus');
var utils = require('../utils');
var Key = require('../key');
var PrivateKey = require('../private-key');

var pem = require('./pem');
var ssh = require('./ssh');
var rfc4253 = require('./rfc4253');

function read(buf, options) {
	if (typeof (buf) === 'string') {
		if (buf.trim().match(/^[-]+[ ]*BEGIN/))
			return (pem.read(buf, options));
		if (buf.match(/^\s*ssh-[a-z]/))
			return (ssh.read(buf, options));
		if (buf.match(/^\s*ecdsa-/))
			return (ssh.read(buf, options));
		buf = new Buffer(buf, 'binary');
	} else {
		assert.buffer(buf);
		if (findPEMHeader(buf))
			return (pem.read(buf, options));
		if (findSSHHeader(buf))
			return (ssh.read(buf, options));
	}
	if (buf.readUInt32BE(0) < buf.length)
		return (rfc4253.read(buf, options));
	throw (new Error('Failed to auto-detect format of key'));
}

function findSSHHeader(buf) {
	var offset = 0;
	while (offset < buf.length &&
	    (buf[offset] === 32 || buf[offset] === 10 || buf[offset] === 9))
		++offset;
	if (offset + 4 <= buf.length &&
	    buf.slice(offset, offset + 4).toString('ascii') === 'ssh-')
		return (true);
	if (offset + 6 <= buf.length &&
	    buf.slice(offset, offset + 6).toString('ascii') === 'ecdsa-')
		return (true);
	return (false);
}

function findPEMHeader(buf) {
	var offset = 0;
	while (offset < buf.length &&
	    (buf[offset] === 32 || buf[offset] === 10))
		++offset;
	if (buf[offset] !== 45)
		return (false);
	while (offset < buf.length &&
	    (buf[offset] === 45))
		++offset;
	while (offset < buf.length &&
	    (buf[offset] === 32))
		++offset;
	if (offset + 5 > buf.length ||
	    buf.slice(offset, offset + 5).toString('ascii') !== 'BEGIN')
		return (false);
	return (true);
}

function write(key, options) {
	throw (new Error('"auto" format cannot be used for writing'));
}

Version data entries

27 entries across 22 versions & 7 rubygems

Version Path
cortex-0.1.3 spec/dummy/node_modules/sshpk/lib/formats/auto.js
affiliator-0.2.1 node_modules/fsevents/node_modules/sshpk/lib/formats/auto.js
lanes-0.8.0 node_modules/sshpk/lib/formats/auto.js
lanes-0.8.0 node_modules/fsevents/node_modules/sshpk/lib/formats/auto.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
node-compiler-0.9.0 vendor/node-v7.2.1/deps/npm/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
select_all-rails-0.3.1 node_modules/sshpk/lib/formats/auto.js
node-compiler-0.8.0 vendor/node-v7.2.0/deps/npm/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
node-compiler-0.7.0 vendor/node-v6.9.1/deps/npm/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
node-compiler-0.7.0 vendor/node-v7.1.0/deps/npm/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.10 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.9 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.8 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.7 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.6 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.5 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.4 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.3 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.2 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js
tck-lambdas-0.3.1 lib/tck/lambdas/chistacojs/source/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/auto.js