├── .gitignore ├── LICENSE.md ├── README.md ├── bin ├── ssh-keygen-32.exe └── ssh-keygen-64.exe ├── package.json ├── src └── ssh-keygen.js ├── test.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | node_modules/* -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Eric Vicenti 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ssh-keygen 2 | ========== 3 | 4 | Generates a SSH key-pair 5 | 6 | ### Install 7 | 1. Make sure you have ssh-keygen (try `$ ssh-keygen` if you aren't sure) 8 | 2. npm package install 9 | 10 | ``` 11 | npm install ssh-keygen 12 | ``` 13 | OR download from github and place in ./node_modules 14 | 15 | ### Usage 16 | 17 | 18 | 19 | ```js 20 | var keygen = require('ssh-keygen'); 21 | var fs = require('fs'); 22 | 23 | var location = __dirname + '/foo_rsa'; 24 | var comment = 'joe@foobar.com'; 25 | var password = 'keypassword'; // false and undefined will convert to an empty pw 26 | var format = 'PEM'; // default is RFC4716 27 | 28 | keygen({ 29 | location: location, 30 | comment: comment, 31 | password: password, 32 | read: true, 33 | format: format 34 | }, function(err, out){ 35 | if(err) return console.log('Something went wrong: '+err); 36 | console.log('Keys created!'); 37 | console.log('private key: '+out.key); 38 | console.log('public key: '+out.pubKey); 39 | }); 40 | 41 | ``` 42 | 43 | The following shell command will get executed: 44 | 45 | ``` 46 | $ ssh-keygen -t rsa -b 2048 -C "joe@foobar.com" -N "keypassword" -m PEM -f ./foo_rsa 47 | Generating public/private rsa key pair. 48 | Your identification has been saved in ./foo_rsa. 49 | Your public key has been saved in ./foo_rsa.pub. 50 | The key fingerprint is: 51 | 02:f7:40:b6:c7:b3:a3:68:16:53:dd:86:63:df:b5:33 joe@foobar.com 52 | The key's randomart image is: 53 | +--[ RSA 2048]----+ 54 | | o | 55 | | o + o | 56 | | . = O o . | 57 | | + = * . . . | 58 | | o . S . . E | 59 | | + o . o | 60 | | + . | 61 | | o | 62 | | | 63 | +-----------------+ 64 | ``` 65 | 66 | ### Parameters 67 | 68 | * location, desired location for the key. The public key will be at the location + `.pub`, defaults temp dir 69 | * read, should the callback have the key files read into it, defaults true 70 | * force, destroy pre-existing files with the location name and the public key name, defaults true 71 | * destroy, destroy the key files once they have been read, defaults false 72 | * comment, the comment that should be embedded into the key, defaults empty 73 | * password, the password for the key, defaults empty 74 | 75 | ### Note 76 | 77 | It is advisable to generate your keys on a machine with a significant random source like one with a mouse/trackpad. 78 | 79 | ### License 80 | 81 | ssh-keygen is [open source](https://github.com/ericvicenti/ssh-keygen/blob/master/LICENSE.md) under the MIT license 82 | 83 | ### Windows 84 | 85 | This package bundles binaries for windows. The current version is: `2.4.4.2-rc3` 86 | 87 | ### Todo 88 | 89 | * Real tests 90 | 91 | Contributors welcome! 92 | -------------------------------------------------------------------------------- /bin/ssh-keygen-32.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericvicenti/ssh-keygen/103377775b040e29115b6fdfc585e890de4f9e63/bin/ssh-keygen-32.exe -------------------------------------------------------------------------------- /bin/ssh-keygen-64.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericvicenti/ssh-keygen/103377775b040e29115b6fdfc585e890de4f9e63/bin/ssh-keygen-64.exe -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ssh-keygen", 3 | "version": "0.5.0", 4 | "author": { 5 | "name": "Eric Vicenti", 6 | "url": "http://github.com/ericvicenti" 7 | }, 8 | "homepage": "http://github.com/ericvicenti/ssh-keygen", 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/ericvicenti/ssh-keygen.git" 12 | }, 13 | "engines": { 14 | "node": ">= 0.6.0" 15 | }, 16 | "keywords": [ 17 | "ssh", 18 | "key", 19 | "pair", 20 | "private", 21 | "public" 22 | ], 23 | "bugs": { 24 | "url": "http://github.com/ericvicenti/ssh-keygen/issues" 25 | }, 26 | "main": "src/ssh-keygen", 27 | "description": "Generates SSH key-pairs", 28 | "dependencies": { 29 | "underscore": "1.4.x" 30 | }, 31 | "licenses": [ 32 | { 33 | "type": "MIT", 34 | "url": "https://github.com/ericvicenti/ssh-keygen/blob/master/LICENSE.md" 35 | } 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /src/ssh-keygen.js: -------------------------------------------------------------------------------- 1 | var spawn = require('child_process').spawn; 2 | var _ = require('underscore'); 3 | var fs = require('fs'); 4 | var os = require('os'); 5 | var path = require('path'); 6 | 7 | var log = function(a){ 8 | if(process.env.VERBOSE) console.log('ssh-keygen: '+a); 9 | } 10 | function binPath() { 11 | if(process.platform !== 'win32') return 'ssh-keygen'; 12 | 13 | switch(process.arch) { 14 | case 'ia32': return path.join(__dirname, '..', 'bin', 'ssh-keygen-32.exe'); 15 | case 'x64': return path.join(__dirname, '..', 'bin', 'ssh-keygen-64.exe'); 16 | } 17 | 18 | throw new Error('Unsupported platform'); 19 | } 20 | function checkAvailability(location, force, callback){ 21 | var pubLocation = location+'.pub'; 22 | log('checking availability: '+location); 23 | fs.exists(location, function(keyExists){ 24 | log('checking availability: '+pubLocation); 25 | fs.exists(pubLocation, function(pubKeyExists){ 26 | doForce(keyExists, pubKeyExists); 27 | }) 28 | }); 29 | function doForce(keyExists, pubKeyExists){ 30 | if(!force && keyExists) return callback(location+' already exists'); 31 | if(!force && pubKeyExists) return callback(pubLocation+' already exists'); 32 | if(!keyExists && !pubKeyExists) return callback(); 33 | if(keyExists){ 34 | log('removing '+location); 35 | fs.unlink(location, function(err){ 36 | if(err) return callback(err); 37 | keyExists = false; 38 | if(!keyExists && !pubKeyExists) callback(); 39 | }); 40 | } 41 | if(pubKeyExists) { 42 | log('removing '+pubLocation); 43 | fs.unlink(pubLocation, function(err){ 44 | if(err) return callback(err); 45 | pubKeyExists = false; 46 | if(!keyExists && !pubKeyExists) callback(); 47 | }); 48 | } 49 | } 50 | } 51 | function ssh_keygen(location, opts, callback){ 52 | opts || (opts={}); 53 | 54 | var pubLocation = location+'.pub'; 55 | if(!opts.comment) opts.comment = ''; 56 | if(!opts.password) opts.password = ''; 57 | if(!opts.size) opts.size = '2048'; 58 | if(!opts.format) opts.format = 'RFC4716'; 59 | 60 | var keygen = spawn(binPath(), [ 61 | '-t','rsa', 62 | '-b', opts.size, 63 | '-C', opts.comment, 64 | '-N', opts.password, 65 | '-f', location, 66 | '-m', opts.format 67 | ]); 68 | 69 | keygen.stdout.on('data', function(a){ 70 | log('stdout:'+a); 71 | }); 72 | 73 | var read = opts.read; 74 | var destroy = opts.destroy; 75 | 76 | keygen.on('exit',function(){ 77 | log('exited'); 78 | if(read){ 79 | log('reading key '+location); 80 | fs.readFile(location, 'utf8', function(err, key){ 81 | if(destroy){ 82 | log('destroying key '+location); 83 | fs.unlink(location, function(err){ 84 | if(err) return callback(err); 85 | readPubKey(); 86 | }); 87 | } else readPubKey(); 88 | function readPubKey(){ 89 | log('reading pub key '+pubLocation); 90 | fs.readFile(pubLocation, 'utf8', function(err, pubKey){ 91 | if(destroy){ 92 | log('destroying pub key '+pubLocation); 93 | fs.unlink(pubLocation, function(err){ 94 | if(err) return callback(err); 95 | key = key.toString(); 96 | key = key.substring(0, key.lastIndexOf("\n")).trim(); 97 | pubKey = pubKey.toString(); 98 | pubKey = pubKey.substring(0, pubKey.lastIndexOf("\n")).trim(); 99 | return callback(undefined, { 100 | key: key, pubKey: pubKey 101 | }); 102 | }); 103 | } else callback(undefined, { key: key, pubKey: pubKey }); 104 | }); 105 | } 106 | }); 107 | } else if(callback) callback(); 108 | }); 109 | 110 | keygen.stderr.on('data',function(a){ 111 | log('stderr:'+a); 112 | }); 113 | }; 114 | 115 | module.exports = function(opts, callback){ 116 | var location = opts.location; 117 | if(!location) location = path.join(os.tmpdir(),'id_rsa'); 118 | 119 | if(_.isUndefined(opts.read)) opts.read = true; 120 | if(_.isUndefined(opts.force)) opts.force = true; 121 | if(_.isUndefined(opts.destroy)) opts.destroy = false; 122 | 123 | checkAvailability(location, opts.force, function(err){ 124 | if(err){ 125 | log('availability err '+err); 126 | return callback(err); 127 | } 128 | ssh_keygen(location, opts, callback); 129 | }); 130 | }; 131 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var keygen = require('./src/ssh-keygen'); 2 | 3 | console.log('Generating key pair') 4 | 5 | keygen({ 6 | comment: 'john@doe.com', 7 | read: true 8 | }, function(err, out){ 9 | if(err) return console.log('There was a problem : '+err); 10 | console.log('Done generating key pairs'); 11 | console.log(out.key) 12 | console.log(out.pubKey) 13 | }); -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | underscore@1.4.x: 6 | version "1.4.4" 7 | resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.4.4.tgz#61a6a32010622afa07963bf325203cf12239d604" 8 | --------------------------------------------------------------------------------