Question

I'm getting a public key from the github api, so it's in the format it needs to be for ssh to read it.

I'd like to get formatted as a PEM so that I can work with it using ruby's OpenSSL library, specifically I need an OpenSSL::PKey::RSA instance.

essentially, I'd like this command in Ruby:

ssh-keygen -f testing_rsa.pub  -e -m pem

I found the SSHKey gem, but it doesn't seem to do this specific thing.

Is there a way to do this with OpenSSL or another library or do I need to resort to what these other answers are suggesting and convert it by hand?

Was it helpful?

Solution

require 'open3'

def key_file_in_pem_format key_file
  stdout, status = Open3::capture2('ssh-keygen', '-f', key_file, '-e', '-m', 'pem')
  raise unless status == 0
  stdout
end

This returns the output of the ssh-keygen program as a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top