Class: SafeDb::Keypair

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/keys/keypair.rb

Overview

This class creates and represents an Elliptic Curve cryptographic key. The generated key can then be comsumed via its various aspects like its ssh formatted public key and/or the pem formatted private key.

Instance Method Summary collapse

Constructor Details

#initializeKeypair

Generate an elliptic curve cryptographic keypair. After the key is generated, both the public and private keys can be retrieved through the accessors.



108
109
110
111
112
113
114
115
# File 'lib/utils/keys/keypair.rb', line 108

def initialize

  @ec_keypair = OpenSSL::PKey::EC.new( Indices::ELLIPTIC_CURVE_KEY_TYPE )
  @ec_keypair.generate_key!

  log.info(x) { "An elliptic curve keypair has just been generated." }

end

Instance Method Details

#private_key_pemString

Get the private key aspect of this elliptic curve cryptographic key in PEM format.

Returns:

  • (String)

    the PEM formatted private key



121
122
123
# File 'lib/utils/keys/keypair.rb', line 121

def private_key_pem()
  return @ec_keypair.to_pem()
end

#public_key_sshString

Get the public key aspect of this elliptic curve cryptographic key in the long line SSH format. This format states the key type which will be ecdsa-sha2-nistp384 followed by base64 encoded data.

The returned one line public key will likely contain forward slashes and possibly equal signs at the end of the string.

Returns:

  • (String)

    the SSH formatted public key prefixed by the key type



134
135
136
137
138
139
# File 'lib/utils/keys/keypair.rb', line 134

def public_key_ssh()
  require 'net/ssh'
  key_type = @ec_keypair.ssh_type()
  key_data = [ @ec_keypair.to_blob ].pack('m0')
  return "#{key_type} #{key_data}"
end