Class: SSLTool::PEMScanner
- Inherits:
-
Object
- Object
- SSLTool::PEMScanner
- Defined in:
- lib/ssltool/pem_scanner.rb
Constant Summary collapse
- RX_PEM_BLOCK =
/(-----BEGIN.*?-----\n (?:[A-Za-z0-9\+\/]+\n)* [A-Za-z0-9\+\/]* (?:=|==|=\n=)? \n -----END.*?----- )/x
Instance Attribute Summary collapse
-
#cert_strings ⇒ Object
readonly
Returns the value of attribute cert_strings.
-
#garbage_strings ⇒ Object
readonly
Returns the value of attribute garbage_strings.
-
#key_strings ⇒ Object
readonly
Returns the value of attribute key_strings.
-
#pem_strings ⇒ Object
readonly
Returns the value of attribute pem_strings.
Class Method Summary collapse
Instance Method Summary collapse
- #certificates ⇒ Object (also: #certs)
-
#initialize(s) ⇒ PEMScanner
constructor
A new instance of PEMScanner.
- #keys ⇒ Object
Constructor Details
#initialize(s) ⇒ PEMScanner
Returns a new instance of PEMScanner.
29 30 31 32 33 34 35 |
# File 'lib/ssltool/pem_scanner.rb', line 29 def initialize(s) s = s.dup.force_encoding('BINARY').gsub(/\r\n?/, "\n").gsub(/\s+\n/, "\n") @pem_strings, @garbage_strings = s.split(RX_PEM_BLOCK).map(&:strip).reject(&:empty?).partition { |s| s =~ RX_PEM_BLOCK } @cert_strings = @pem_strings.select { |s| s =~ /-----BEGIN CERTIFICATE-----/ } @key_strings = @pem_strings.select { |s| s =~ /-----BEGIN (EC |RSA )?PRIVATE KEY-----/ } @garbage_strings += @pem_strings - @cert_strings - @key_strings end |
Instance Attribute Details
#cert_strings ⇒ Object (readonly)
Returns the value of attribute cert_strings.
7 8 9 |
# File 'lib/ssltool/pem_scanner.rb', line 7 def cert_strings @cert_strings end |
#garbage_strings ⇒ Object (readonly)
Returns the value of attribute garbage_strings.
7 8 9 |
# File 'lib/ssltool/pem_scanner.rb', line 7 def garbage_strings @garbage_strings end |
#key_strings ⇒ Object (readonly)
Returns the value of attribute key_strings.
7 8 9 |
# File 'lib/ssltool/pem_scanner.rb', line 7 def key_strings @key_strings end |
#pem_strings ⇒ Object (readonly)
Returns the value of attribute pem_strings.
7 8 9 |
# File 'lib/ssltool/pem_scanner.rb', line 7 def pem_strings @pem_strings end |
Class Method Details
.certs_from(s) ⇒ Object
24 25 26 |
# File 'lib/ssltool/pem_scanner.rb', line 24 def certs_from(s) scan(s).certs end |
.keys_from(s) ⇒ Object
20 21 22 |
# File 'lib/ssltool/pem_scanner.rb', line 20 def keys_from(s) scan(s).keys end |
Instance Method Details
#certificates ⇒ Object Also known as: certs
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ssltool/pem_scanner.rb', line 37 def certificates @certificates ||= cert_strings.map do |s| begin Certificate.new(s) rescue OpenSSL::X509::CertificateError garbage_strings << cert_strings.delete(s) nil end end.compact end |