Skip to content

Commit

Permalink
Added key reading to callback, added forcing, opts with default location
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Mar 24, 2013
1 parent 2252a2d commit b175d73
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
28 changes: 19 additions & 9 deletions README.md
Expand Up @@ -24,15 +24,16 @@ var location = __dirname + '/foo_rsa';
var comment = 'joe@foobar.com';
var password = 'keypassword'; // false and undefined will convert to an empty pw
keygen(location, comment, {
password: password
}, function(err){
if(err) return console.log('Something went wrong!');
console.log('Keys created!')
var private = fs.readFileSync( location );
var public = fs.readFileSync( location + '.pub');
console.log('private key: '+private);
console.log('public key: '+public);
keygen({
location: location,
comment: comment,
password: password,
read: true
}, function(err, out){
if(err) return console.log('Something went wrong: '+err);
console.log('Keys created!');
console.log('private key: '+out.key);
console.log('public key: '+out.pubKey);
});
```
Expand Down Expand Up @@ -60,6 +61,15 @@ The key's randomart image is:
+-----------------+
```

### Parameters

* location, desired location for the key. The public key will be at the location + `.pub`
* read, should the callback have the key files read into it, defaults true
* force, destroy pre-existing files with the location name and the public key name, defaults true
* destroy, destroy the key files once they have been read
* comment, the comment that should be embedded into the key
* password, the password for the key, defaults empty

### Note

It is advisable to generate your keys on a machine with a significant random source like one with a mouse/trackpad.
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ssh-keygen",
"version": "0.1.0",
"version": "0.2.0",
"author": {
"name": "Eric Vicenti",
"url": "http://github.com/ericvicenti"
Expand Down
91 changes: 78 additions & 13 deletions src/ssh-keygen.js
@@ -1,19 +1,53 @@
var spawn = require('child_process').spawn;
var _ = require('underscore');
var fs = require('fs');
var os = require('os');

var log = function(a){
if(process.env.VERBOSE) console.log('ssh-keygen: '+a);
}

function ssh_keygen(location, comment, opts, callback){
function checkAvailability(location, force, callback){
var pubLocation = location+'.pub';
log('checking availability: '+location);
fs.exists(location, function(keyExists){
log('checking availability: '+pubLocation);
fs.exists(pubLocation, function(pubKeyExists){
doForce(keyExists, pubKeyExists);
})
});
function doForce(keyExists, pubKeyExists){
if(!force && keyExists) return callback(location+' already exists');
if(!force && pubKeyExists) return callback(pubLocation+' already exists');
if(!keyExists && !pubKeyExists) return callback();
if(keyExists){
log('removing '+location);
fs.unlink(location, function(err){
if(err) return callback(err);
keyExists = false;
if(!keyExists && !pubKeyExists) callback();
});
}
if(pubKeyExists) {
log('removing '+pubLocation);
fs.unlink(pubLocation, function(err){
if(err) return callback(err);
pubKeyExists = false;
if(!keyExists && !pubKeyExists) callback();
});
}
}
}
function ssh_keygen(location, opts, callback){
opts || (opts={});
if(!comment) comment = '';

var pubLocation = location+'.pub';
if(!opts.comment) opts.comment = '';
if(!opts.password) opts.password = '';

var keygen = spawn('ssh-keygen', [
'-t','rsa',
'-b','2048',
'-C', comment,
'-C', opts.comment,
'-N', opts.password,
'-f', location
]);
Expand All @@ -22,23 +56,54 @@ function ssh_keygen(location, comment, opts, callback){
log('stdout:'+a);
});

var read = opts.read;
var destroy = opts.destroy;

keygen.on('exit',function(){
log('exited');
if(callback) callback();
if(read){
log('reading key '+location);
fs.readFile(location, {encoding: 'utf8'}, function(err, key){
if(destroy){
log('destroying key '+location);
fs.unlink(location, function(err){
if(err) return callback(err);
readPubKey();
});
} else readPubKey();
function readPubKey(){
log('reading pub key '+pubLocation);
fs.readFile(pubLocation, {encoding: 'utf8'}, function(err, pubKey){
if(destroy){
log('destroying pub key '+pubLocation);
fs.unlink(pubLocation, function(err){
if(err) return callback(err);
return callback(undefined, { key: key, pubKey: pubKey });
});
} else callback(undefined, { key: key, pubKey: pubKey });
});
}
});
} else if(callback) callback();
});

keygen.stderr.on('data',function(a){
log('stderr:'+a);
});
};

module.exports = function(location, comment, opts, callback){
// Make sure the keys dont already exist
fs.exists(location, function(exists){
if(exists) return callback(true);
fs.exists(location+'.pub', function(exists){
if(exists) return callback(true);
ssh_keygen(location, comment, opts, callback);
})
module.exports = function(opts, callback){
var location = opts.location;
if(!location) location = os.tmpDir()+'id_rsa';

if(_.isUndefined(opts.read)) opts.read = true;
if(_.isUndefined(opts.force)) opts.force = true;

checkAvailability(location, opts.force, function(err){
if(err){
log('availability err '+err);
return callback(err);
}
ssh_keygen(location, opts, callback);
});
};
12 changes: 9 additions & 3 deletions test.js
@@ -1,7 +1,13 @@
var keygen = require('./src/ssh-keygen');

console.log('Generating key pair')
keygen(__dirname + '/foobar_rsa', 'john@doe.com', 'keyPassword', function(err){
if(err) console.log('There was a problem');
else console.log('Done generating key pairs in '+__dirname);

keygen({
comment: 'john@doe.com',
read: true
}, function(err, out){
if(err) return console.log('There was a problem : '+err);
console.log('Done generating key pairs');
console.log(out.key)
console.log(out.pubKey)
});

0 comments on commit b175d73

Please sign in to comment.