Class: CertificateDepot::Keypair

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

Overview

Represents an OpenSSL RSA key. Because RSA is part of a PKI the private key is usually paired with the public key.

Constant Summary collapse

DEFAULT_LENGTH =
2048

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key = nil) ⇒ Keypair

Instantiate a new Keypair with a private key. The private key should be an instance of OpenSSL::PKey::RSA.


11
12
13
# File 'lib/certificate_depot/keypair.rb', line 11

def initialize(private_key=nil)
  @private_key = private_key
end

Instance Attribute Details

#private_keyObject

Returns the value of attribute private_key


7
8
9
# File 'lib/certificate_depot/keypair.rb', line 7

def private_key
  @private_key
end

Class Method Details

.generateObject

Shortcut method to generate a new keypair.

keypair = CertificateDepot::Keypair.generate
keypair.write_to('/var/lib/depot/storage/my-key.key')

37
38
39
40
41
# File 'lib/certificate_depot/keypair.rb', line 37

def self.generate
  keypair = new
  keypair.generate
  keypair
end

Instance Method Details

#generateObject

Generates a new private and public keypair.


16
17
18
# File 'lib/certificate_depot/keypair.rb', line 16

def generate
  @private_key = OpenSSL::PKey::RSA.generate(DEFAULT_LENGTH)
end

#public_keyObject

Returns the public key


21
22
23
# File 'lib/certificate_depot/keypair.rb', line 21

def public_key
  @private_key.public_key
end

#write_to(path) ⇒ Object

Writes the keypair to file. The path should be a filename pointing to an existing directory. Note that this will overwrite files without asking.


28
29
30
31
# File 'lib/certificate_depot/keypair.rb', line 28

def write_to(path)
  File.open(path, 'w') { |file| file.write(@private_key.to_pem) }
  File.chmod(0400, path)
end