Skip to content

Instantly share code, notes, and snippets.



Last active Sep 11, 2015
What would you like to do?
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