Module: R509::Helpers

Included in:
CSR, Cert, SPKI
Defined in:
lib/r509/helpers.rb

Overview

Various helper methods to reduce duplication across classes. These methods are used in the Cert, CSR, SPKI, and PrivateKey classes.

Instance Method Summary (collapse)

Instance Method Details

- (Integer) bit_length Also known as: bit_strength

Returns the bit length of the key

Returns:

  • (Integer)

    the integer bit length.



54
55
56
57
58
59
60
61
62
# File 'lib/r509/helpers.rb', line 54

def bit_length
  if self.rsa?
    return internal_obj.public_key.n.num_bits
  elsif self.dsa?
    return internal_obj.public_key.p.num_bits
  elsif self.ec?
    raise R509::R509Error, 'Bit length is not available for EC at this time.'
  end
end

- (String) curve_name

Returns the short name of the elliptic curve used to generate the public key if the key is EC. If not, raises an error.

Returns:

  • (String)

    elliptic curve name



44
45
46
47
48
49
50
# File 'lib/r509/helpers.rb', line 44

def curve_name
  if self.ec?
    internal_obj.public_key.group.curve_name
  else
    raise R509::R509Error, 'Curve name is only available with EC'
  end
end

- (Boolean) dsa?

Returns whether the public key is DSA

Returns:

  • (Boolean)

    true if the public key is DSA, false otherwise



16
17
18
# File 'lib/r509/helpers.rb', line 16

def dsa?
  internal_obj.public_key.kind_of?(OpenSSL::PKey::DSA)
end

- (Boolean) ec?

Returns whether the public key is EC

Returns:

  • (Boolean)

    true if the public key is EC, false otherwise



23
24
25
# File 'lib/r509/helpers.rb', line 23

def ec?
  internal_obj.public_key.kind_of?(OpenSSL::PKey::EC)
end

- (String) key_algorithm

Returns key algorithm (RSA/DSA/EC)

Returns:

  • (String)

    value of the key algorithm.



30
31
32
33
34
35
36
37
38
# File 'lib/r509/helpers.rb', line 30

def key_algorithm
  if self.rsa?
    "RSA"
  elsif self.dsa?
    "DSA"
  elsif self.ec?
    "EC"
  end
end

- (Boolean) rsa?

Returns whether the public key is RSA

Returns:

  • (Boolean)

    true if the public key is RSA, false otherwise



9
10
11
# File 'lib/r509/helpers.rb', line 9

def rsa?
  internal_obj.public_key.kind_of?(OpenSSL::PKey::RSA)
end

- (String) to_der

Converts the object into DER format

Returns:

  • (String)

    the object converted into DER format.



89
90
91
# File 'lib/r509/helpers.rb', line 89

def to_der
    internal_obj.to_der
end

- (String) to_pem

Converts the object into PEM format

Returns:

  • (String)

    the object converted into PEM format.



82
83
84
# File 'lib/r509/helpers.rb', line 82

def to_pem
    internal_obj.to_pem
end

- (Object) write_der(filename_or_io)

Writes the object into DER format

Parameters:

  • filename_or_io (String, #write)

    Either a string of the path for the file that you'd like to write, or an IO-like object.



75
76
77
# File 'lib/r509/helpers.rb', line 75

def write_der(filename_or_io)
  write_data(filename_or_io, internal_obj.to_der)
end

- (Object) write_pem(filename_or_io)

Writes the object into PEM format

Parameters:

  • filename_or_io (String, #write)

    Either a string of the path for the file that you'd like to write, or an IO-like object.



68
69
70
# File 'lib/r509/helpers.rb', line 68

def write_pem(filename_or_io)
  write_data(filename_or_io, internal_obj.to_pem)
end