Created
April 6, 2019 00:02
-
-
Save KonnorRogers/05957ffe1cd636447966f9489395fea2 to your computer and use it in GitHub Desktop.
SSH key generation in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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