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

#bit_lengthInteger Also known as: bit_strength

Returns the bit length of the key

Returns:

  • (Integer)

    the integer bit length.



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

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

#curve_nameString

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



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

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

#dsa?Boolean

Returns whether the public key is DSA

Returns:

  • (Boolean)

    true if the public key is DSA, false otherwise



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

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

#ec?Boolean

Returns whether the public key is EC

Returns:

  • (Boolean)

    true if the public key is EC, false otherwise



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

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

#key_algorithmString

Returns key algorithm (RSA/DSA/EC)

Returns:

  • (String)

    value of the key algorithm.



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

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

#rsa?Boolean

Returns whether the public key is RSA

Returns:

  • (Boolean)

    true if the public key is RSA, false otherwise



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

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

#to_derString

Converts the object into DER format

Returns:

  • (String)

    the object converted into DER format.



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

def to_der
  internal_obj.to_der
end

#to_pemString

Converts the object into PEM format

Returns:

  • (String)

    the object converted into PEM format.



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

def to_pem
  internal_obj.to_pem
end

#write_der(filename_or_io) ⇒ Object

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.



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

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

#write_pem(filename_or_io) ⇒ Object

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.



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

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