Module: R509::Helpers
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
-
#bit_length ⇒ Integer
(also: #bit_strength)
Returns the bit length of the key.
-
#curve_name ⇒ String
Returns the short name of the elliptic curve used to generate the public key if the key is EC.
-
#dsa? ⇒ Boolean
Returns whether the public key is DSA.
-
#ec? ⇒ Boolean
Returns whether the public key is EC.
-
#key_algorithm ⇒ String
Returns key algorithm (RSA/DSA/EC).
-
#rsa? ⇒ Boolean
Returns whether the public key is RSA.
-
#to_der ⇒ String
Converts the object into DER format.
-
#to_pem ⇒ String
Converts the object into PEM format.
-
#write_der(filename_or_io) ⇒ Object
Writes the object into DER format.
-
#write_pem(filename_or_io) ⇒ Object
Writes the object into PEM format.
Instance Method Details
#bit_length ⇒ Integer Also known as: bit_strength
Returns the bit length of the key
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_name ⇒ String
Returns the short name of the elliptic curve used to generate the public key if the key is EC. If not, raises an error.
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
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
22 23 24 |
# File 'lib/r509/helpers.rb', line 22 def ec? internal_obj.public_key.is_a?(OpenSSL::PKey::EC) end |
#key_algorithm ⇒ String
Returns key algorithm (RSA/DSA/EC)
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
8 9 10 |
# File 'lib/r509/helpers.rb', line 8 def rsa? internal_obj.public_key.is_a?(OpenSSL::PKey::RSA) end |
#to_der ⇒ String
Converts the object into DER format
88 89 90 |
# File 'lib/r509/helpers.rb', line 88 def to_der internal_obj.to_der end |
#to_pem ⇒ String
Converts the object 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
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
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 |