Class: RightScale::RsaKeyPair

Inherits:
Object
  • Object
show all
Defined in:
lib/right_agent/security/rsa_key_pair.rb

Overview

Allows generating RSA key pairs and extracting public key component Note: Creating a RSA key pair can take a fair amount of time (seconds)

Constant Summary collapse

DEFAULT_LENGTH =
2048

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length = DEFAULT_LENGTH) ⇒ RsaKeyPair

Create new RSA key pair using ‘length’ bits



35
36
37
# File 'lib/right_agent/security/rsa_key_pair.rb', line 35

def initialize(length = DEFAULT_LENGTH)
  @raw_key = OpenSSL::PKey::RSA.generate(length)
end

Instance Attribute Details

#raw_keyObject (readonly)

Underlying OpenSSL keys



32
33
34
# File 'lib/right_agent/security/rsa_key_pair.rb', line 32

def raw_key
  @raw_key
end

Class Method Details

.from_data(data) ⇒ Object

Load key pair previously serialized via ‘data’



56
57
58
59
60
# File 'lib/right_agent/security/rsa_key_pair.rb', line 56

def self.from_data(data)
  res = RsaKeyPair.allocate
  res.instance_variable_set(:@raw_key, OpenSSL::PKey::RSA.new(data)) 
  res
end

.load(file) ⇒ Object

Load key from file



63
64
65
# File 'lib/right_agent/security/rsa_key_pair.rb', line 63

def self.load(file)
  from_data(File.read(file))
end

Instance Method Details

#dataObject Also known as: to_s

Key material in PEM format



50
51
52
# File 'lib/right_agent/security/rsa_key_pair.rb', line 50

def data
  raw_key.to_pem
end

#has_private?Boolean

Does key pair include private key?

Returns:

  • (Boolean)


40
41
42
# File 'lib/right_agent/security/rsa_key_pair.rb', line 40

def has_private?
  raw_key.private?
end

#save(file) ⇒ Object

Save key to file in PEM format



68
69
70
71
72
# File 'lib/right_agent/security/rsa_key_pair.rb', line 68

def save(file)
  File.open(file, "w") do |f|
    f.write(@raw_key.to_pem)
  end
end

#to_publicObject

New RsaKeyPair instance with identical public key but no private key



45
46
47
# File 'lib/right_agent/security/rsa_key_pair.rb', line 45

def to_public
  RsaKeyPair.from_data(raw_key.public_key.to_pem)
end