ruby/example_code/s3/s3-ruby-example-create-rsa-keys.rb
# Creates a public and private key file pair.
#
# @param public_key_file [File] An instance of the public key file.
# @param private_key_file [File] An instance of the private key file.
# @param passphrase [String] A passphrase for the private key file.
# @return [Boolean] true if the public and private key files were created;
#   otherwise, false.
# @example
#   public_key_file = File.new('public_key.pem', 'w')
#   private_key_file = File.new('private_key.pem', 'w')
#   exit 1 unless public_and_private_key_created?(
#     public_key_file,
#     private_key_file,
#     'my-passphrase'
#   )

Creates a public and private key file pair.
Creates a public and private key file pair .
def public_and_private_key_created?(
  public_key_file,
  private_key_file,
  passphrase
)
  key = OpenSSL::PKey::RSA.new(2048)
  public_key_file.write(key.public_key.to_pem)
  public_key_file.close
  cipher = OpenSSL::Cipher.new('AES-128-CBC')
  key_secure = key.export(cipher, passphrase)
  private_key_file.write(key_secure)
  private_key_file.close
  return true
rescue StandardError => e
  puts 'Could not create the public key file, the private key file, ' \
    "or both: #{e.message}"
end
def public_and_private_key_created? ( public_key_file , private_key_file , passphrase ) key = OpenSSL :: PKey :: RSA . new ( 2048 ) public_key_file . write ( key . public_key . to_pem ) public_key_file . close cipher = OpenSSL :: Cipher . new ( 'AES-128-CBC' ) key_secure = key . export ( cipher , passphrase ) private_key_file . write ( key_secure ) private_key_file . close return true rescue StandardError => e puts 'Could not create the public key file, the private key file, ' "or both: #{e.message}" end
Creates a public and private key file pair.
Creates a public and private key file pair .
true if the public and private key files were created; otherwise, false.
true if the public and private key files were created ; otherwise false .
Boolean
public_key_file
An instance of the public key file.
An instance of the public key file .
private_key_file
An instance of the private key file.
An instance of the private key file .
passphrase
A passphrase for the private key file.
A passphrase for the private key file .
example
public_key_file = File.new('public_key.pem', 'w') private_key_file = File.new('private_key.pem', 'w') exit 1 unless public_and_private_key_created?( public_key_file, private_key_file, 'my-passphrase' )
public_key_file = File . new ( ' public_key . pem ' ' w ' ) private_key_file = File . new ( ' private_key . pem ' ' w ' ) exit 1 unless public_and_private_key_created? ( public_key_file private_key_file ' my - passphrase ' )