Class: Xml::Kit::KeyPair

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/kit/key_pair.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(certificate, private_key, passphrase, use) ⇒ KeyPair

Returns a new instance of KeyPair.



10
11
12
13
14
15
16
17
18
19
# File 'lib/xml/kit/key_pair.rb', line 10

def initialize(certificate, private_key, passphrase, use)
  @certificate = ::Xml::Kit::Certificate.new(certificate, use: use)
  @private_key =
    if passphrase.present?
      OpenSSL::PKey::RSA.new(private_key, passphrase)
    else
      OpenSSL::PKey::RSA.new(private_key)
    end
  @public_key = @private_key.public_key
end

Instance Attribute Details

#certificateObject (readonly)

Returns the value of attribute certificate.



6
7
8
# File 'lib/xml/kit/key_pair.rb', line 6

def certificate
  @certificate
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



7
8
9
# File 'lib/xml/kit/key_pair.rb', line 7

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



8
9
10
# File 'lib/xml/kit/key_pair.rb', line 8

def public_key
  @public_key
end

Class Method Details

.generate(use:, passphrase: SecureRandom.uuid, algorithm: ::Xml::Kit::Crypto::SymmetricCipher::DEFAULT_ALGORITHM) ⇒ Object

Returns a generated self signed certificate with private key.

Parameters:

  • use (Symbol)

    Can be either ‘:signing` or `:encryption`.

  • passphrase (String) (defaults to: SecureRandom.uuid)

    the passphrase to use to encrypt the private key.

  • algorithm (String) (defaults to: ::Xml::Kit::Crypto::SymmetricCipher::DEFAULT_ALGORITHM)

    the symmetric algorithm to use for encrypting the private key.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xml/kit/key_pair.rb', line 33

def self.generate(
  use:,
  passphrase: SecureRandom.uuid,
  algorithm: ::Xml::Kit::Crypto::SymmetricCipher::DEFAULT_ALGORITHM
)
  algorithm = ::Xml::Kit::Crypto::SymmetricCipher::ALGORITHMS[algorithm]
  certificate, private_key = SelfSignedCertificate.new.create(
    algorithm: algorithm,
    passphrase: passphrase
  )
  new(certificate, private_key, passphrase, use)
end

Instance Method Details

#for?(use) ⇒ Boolean

Returns true if the key pair is the designated use.

Parameters:

  • use (Symbol)

    Can be either ‘:signing` or `:encryption`.

Returns:

  • (Boolean)


24
25
26
# File 'lib/xml/kit/key_pair.rb', line 24

def for?(use)
  certificate.for?(use)
end