Search code examples
encryptioncryptographyrsaopensshforge

Convert ssh public keys from various formats to Open SSH with node-forge


I'd like to convert ssh public keys provided in various formats into the Open SSH format using node-forge.

I have no problem generating the keys and converting with:

forge.ssh.publicKeyToOpenSSH(key, comment);

but cannot figure out how to read the files into node-forge, Tried:

pki.publicKeyFromPem(pem);

or

pki.publicKeyFromAsn1(subjectPublicKeyInfo);

Solution


  • You can load OpenSSL public keys (PKCS#8) like this:

    var forgePubKey = forge.pki.publicKeyFromPem('-----BEGIN PUBLIC KEY-----' + publicKey + '-----END PUBLIC KEY-----');
    

    where publicKey is BASE64 encoded