module Ronin::Support::Crypto::Key

Top-level methods for working with public/private keys.

Public Class Methods

load(key,**kwargs) click to toggle source

Alias for {parse}.

@param [String] key

The PEM or DER encoded key string.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {parse}.

@option kwargs [String, nil] :password

Optional password to decrypt the key.

@return [DH, DSA, EC, RSA]

The parsed key.

@see parse

@api public

# File lib/ronin/support/crypto/key.rb, line 86
def self.load(key,**kwargs)
  parse(key,**kwargs)
end
load_file(path) click to toggle source

Loads the key from the file.

@param [String] path

The path to the key file.

@return [DH, DSA, EC, RSA]

The loaded key.

@raise [ArgumentError]

The key type could not be determined from the key file.

@api public

# File lib/ronin/support/crypto/key.rb, line 104
def self.load_file(path)
  parse(File.read(path))
end
parse(key, password: nil) click to toggle source

Parses an PEM encoded key.

@param [String] key

The PEM or DER encoded key string.

@param [String, nil] password

Optional password to decrypt the key.

@return [OpenSSL::PKey]

The parsed key.

@return [DH, DSA, EC, RSA]

The parsed key.

@raise [ArgumentError]

The key type could not be determined from the key file.

@api public

# File lib/ronin/support/crypto/key.rb, line 51
def self.parse(key, password: nil)
  key_class = if key.start_with?('-----BEGIN RSA PRIVATE KEY-----')
                RSA
              elsif key.start_with?('-----BEGIN DSA PRIVATE KEY-----')
                DSA
              elsif key.start_with?('-----BEGIN DH PARAMETERS-----')
                DH
              elsif key.start_with?('-----BEGIN EC PRIVATE KEY-----')
                EC
              else
                raise(ArgumentError,"cannot determine the key type for key: #{key.inspect}")
              end

  key_class.parse(key, password: password)
end