module R509::Helpers

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

Public Instance Methods

bit_length() click to toggle source

Returns the bit length of the key @return [Integer] the integer bit length.

# 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
Also aliased as: bit_strength
bit_strength()
Alias for: bit_length
curve_name() click to toggle source

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

@return [String] elliptic curve name

# 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?() click to toggle source

Returns whether the public key is DSA

@return [Boolean] true if the public key is DSA, false otherwise

# File lib/r509/helpers.rb, line 15
def dsa?
  internal_obj.public_key.is_a?(OpenSSL::PKey::DSA)
end
ec?() click to toggle source

Returns whether the public key is EC

@return [Boolean] true if the public key is EC, false otherwise

# File lib/r509/helpers.rb, line 22
def ec?
  internal_obj.public_key.is_a?(OpenSSL::PKey::EC)
end
internal_obj() click to toggle source

@private

# File lib/r509/helpers.rb, line 103
def internal_obj
  raise R509::R509Error, "Internal object for helpers not implemented"
end
key_algorithm() click to toggle source

Returns key algorithm (RSA/DSA/EC)

@return [String] value of the key algorithm.

# File lib/r509/helpers.rb, line 29
def key_algorithm
  if self.rsa?
    "RSA"
  elsif self.dsa?
    "DSA"
  elsif self.ec?
    "EC"
  end
end
load_private_key(opts) click to toggle source

@private

# File lib/r509/helpers.rb, line 93
def load_private_key(opts)
  return unless opts.key?(:key)
  if opts[:key].is_a?(R509::PrivateKey)
    return opts[:key]
  else
    return R509::PrivateKey.new(:key => opts[:key])
  end
end
rsa?() click to toggle source

Returns whether the public key is RSA

@return [Boolean] true if the public key is RSA, false otherwise

# File lib/r509/helpers.rb, line 8
def rsa?
  internal_obj.public_key.is_a?(OpenSSL::PKey::RSA)
end
to_der() click to toggle source

Converts the object into DER format

@return [String] the object converted into DER format.

# File lib/r509/helpers.rb, line 88
def to_der
  internal_obj.to_der
end
to_pem() click to toggle source

Converts the object into PEM format

@return [String] the object converted into PEM format.

# File lib/r509/helpers.rb, line 81
def to_pem
  internal_obj.to_pem
end
write_der(filename_or_io) click to toggle source

Writes the object into DER format @param [String, write] filename_or_io Either a string of the path for

the file that you'd like to write, or an IO-like object.
# 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) click to toggle source

Writes the object into PEM format @param [String, write] filename_or_io Either a string of the path for

the file that you'd like to write, or an IO-like object.
# File lib/r509/helpers.rb, line 67
def write_pem(filename_or_io)
  write_data(filename_or_io, internal_obj.to_pem)
end