Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Last active September 11, 2015 19:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-mapper/3572c4d148feae15060f to your computer and use it in GitHub Desktop.
Save max-mapper/3572c4d148feae15060f to your computer and use it in GitHub Desktop.
generate rsa keypair and format public key in openssh format in javascript
// warning you probably want `ssh-keygen -t ed25519` for most use cases. this method lacks secure prng and uses weak rsa
var keypair = require('keypair')
var forge = require('node-forge')
// generates new keypair (slow)
var pair = keypair()
// creates intermediate forge key object
var publicKey = forge.pki.publicKeyFromPem(pair.public)
// formats in openssh format, e.g. compatible with authorized_keys file
var publicKeyOpenSSH = forge.ssh.publicKeyToOpenSSH(publicKey, 'user@domain.tld')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment