easy-keygen

Easy to use ssh-keygen for Node.JS

Usage no npm install needed!

<script type="module">
  import easyKeygen from 'https://cdn.skypack.dev/easy-keygen';
</script>

README

Easy-keygen

Easy to use, multiplataform ssh-keygen for Node.JS. Can return the keys as string or as buffer.

Requirements

Linux /MaOS

  • ES2015

Windows

  • GitBash
  • "C:\Program Files\Git\usr\bin" must be available on the PATH environment variable.

Usage

$ npm install easy-keygen
const keygen = require('easy-keygen');
keygen()
.then(keys => {
  console.log(keys.privateKey, keys.publicKey);
})
.catch(e => console.log(e));
/*
  Will return something like beneth:
 -----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA3dTa07ltbtGjy0E31fNuvJAzSZXArgQVuAOlIN+/z8z9d54b
v8LqcfT36x5TtLVpcNom0PLF3KLbvsk707HzcpIBXcfJc0/Wqb7EfrwcIlfC8f97
2N/5FhUMXOgSWXNkDtKpevQ3yQmuUlDvp3gaJ9e/ZVNExYydRm/W5Rg17NOXP/xL
LPhcdAko/VlwOACI14E1N8mZmO+/NxAJiTAVH1bHVKbnN+MOIETrYTrWoMJ6zSM9
j5lzFz/C7/EQIH2/0qRMLeFSzgUN4Sr3TCZ61L0ab2ceAesXjN28fuoAOuBRpq15
3KY268bWV57OXrHS554mYFO0OYUYAIVutn5v8QIDAQABAoIBAFl24fYnv4HFHLj5
iYpJDi0QPw77TWUhEp1PIwa6FoK5GrRmWKyi8Kia8m2ponqr+2VfJe55KuFL8THl
uLIs/5ECX3ix/YX6+gklfdvbA8G6AU5WCl2yHqKIlxcpJVVcG0t7HI0Q93ql498g
8zZoUqwk7/Tuezrs9fUVX+avN/hVsP1ia4Pe4RGLSt2TAWPxo12ObO18SdBmfX6g
eo30r/JuqaOiTCpLvJlqBCyfcHTQOwaI7XenfEddKE1im80Avh4UtY0UwR4H5ye5
1mA1gsfMEb1NK9s9RvO0w8ba3msUzFw+s8dG6gq7hrhh5jNtw9bcKiPrXzYZs254
2TwR3cECgYEA/dJrpI5RS1kNV5NV+i8yVW6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lU0TFjJ1Gb9blGDXs05c//Ess+Fx0CSj9WXA4AIjXgTU3yZmY778zEAmJMBUfVsdUpuc34w4g
ROertagwnrNIz2PmXMXP8Lv8RAgfb/SpEwt4VLOBQ3hKvdMJnrUvRpvZx4B6xeM3bx+6gA64FGmrXncpjbrxtZXn
s5esdLnniZgU7Q5hRgAhW62fm/x someuser@Somecomputer-1234-123
*/

With no arguments the key pair will no be persisted, to save the key pair you need to pass a path like beneth:

const keygen = require('easy-keygen');
const fs = require('fs');

keygen(`${__dirname}/cert`)
.then(keys => {
  console.log(keys.privateKey, keys.publicKey);
})
.catch(e => console.log(e));
/**
  - This wil have the same result as the aobe, but the certificates will be saved as cert,
  and cert.pub
*/

Warning: If the path for your cetificates are the same of any other files with the same name they will be erased unless you set options.prevent = true. In this case there will be an error.

Options

You cam pass an object with one to five options

  • Buffer: {'buffer': true}. Will return the key im buffer format, instead of string. Usefull for payload generation.
  • Authentication type: {'type': 'rsa'}. Is the type of authentivation on the certificate that will be generated. The default is RSA. (refere to ssh-keygen -? or Choosing_the_authentication_key_type for more information on SSH keys authentication types.)
  • Passphrase: {'passphrase': 'secret phrase goes here'}. Is the passprase. It will be required for future decription if set.
  • SSH key size: {'size': 1024}. Sets the size, in bits, of the key pair genareated, the bigger the size longer the it will take to genarate the key pair. (A long key pair with 9216 may take over 5 seconds, depending on the sistema resources.)
  • Prevent rewrite: {'prevent': true}. Will prevent the re-write of the file with the same name as the end of the path option. (The script may freeze case there is an file with the same name.)
const keygen = require('easy-keygen');
const fs = require('fs');

keygen(`${__dirname}/cert`, {
  'passphrase': 'any string does it',
  'type': 'dsa',
  'size': 3096,
  'prevent': true
})
.then(keys => {
  console.log(keys.privateKey, keys.publicKey);
})
.catch(e => console.log(e));
/**
  - This wil have a result similar to the first example, but the certificates will be saved as cert, and cert.pub, will
  have 3096 bits oz size, and will be DSA type.
*/

To return a buffer data

const keygen = require('easy-keygen');
const fs = require('fs');

keygen(`${__dirname}/cert`, {
  'buffer': true
})
.then(keys => {
  console.log(keys.privateKey, keys.publicKey);
})
.catch(e => console.log(e));
/**
  Will return something like:
  <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 52 53 41 20 50 52 49 56 41 54 45 20 4b 45 59 2d 2d 2d 2d 2d 0a
  4d 49 49 45 6f 77 49 42 41 41 4b 43 41 51 45 41 73 6b ... > <Buffer 73 73 68 2d 72 73 61 20 41 41 41 41
  42 33 4e 7a 61 43 31 79 63 32 45 41 41 41 41 44 41 51 41 42 41 41 41 42 41 51 43 79 53 69 39 65 4a 4a 31
  50 76 68 ... >
*/

Testing

$ npm test
/**
 the dev dependencies must be installed.
*/