Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Created April 6, 2019 00:02
Show Gist options
  • Save KonnorRogers/05957ffe1cd636447966f9489395fea2 to your computer and use it in GitHub Desktop.
Save KonnorRogers/05957ffe1cd636447966f9489395fea2 to your computer and use it in GitHub Desktop.
SSH key generation in ruby
# This isnt meant to provide all options, just my simple wrapper for personal purposes
def generate_ssh_key
type = 'rsa'
bits = 4096
comment = 'example@example.com'
system("ssh-keygen -t #{type} -b #{bits} -C #{comment}")
end
# If you wanna specify an output file
def generate_ssh_key_with_file
type = 'rsa'
bits = 4096
comment = 'example@example.com'
output = '/home/user/example_user'
system("ssh-keygen -t #{type} -b #{bits} -C #{comment} -f #{output}")
end
## All one method
def generate_ssh_key_with_file(output = nil)
type = 'rsa'
bits = 4096
comment = 'example@example.com'
output = " -f #{output}" if output
system("ssh-keygen -t #{type} -b #{bits} -C #{comment}#{output}")
end
require 'openssl'
key = OpenSSL::PKey::RSA.new(4096)
open('private_key.pem', 'w') { |io| io.write key.to_pem }
open('public_key.pem', 'w') { |io| io.write key.public_key.to_pem }
#####################################################################
# Unfortunately, this only purely generates an RSA key #
# Based on my findings, I could either load a libray like Net::SSH #
# Or simply write a wrapped around ssh-keygen #
#####################################################################
# After looking @
# https://github.com/bensie/sshkey/blob/master/lib/sshkey.rb
# It appears to create a pure ruby implementation is a little much
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment