Module: Ronin::Support::Crypto::Key

Defined in:
lib/ronin/support/crypto/key.rb,
lib/ronin/support/crypto/key/dh.rb,
lib/ronin/support/crypto/key/ec.rb,
lib/ronin/support/crypto/key/dsa.rb,
lib/ronin/support/crypto/key/rsa.rb,
lib/ronin/support/crypto/key/methods.rb

Overview

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

Since:

  • 1.0.0

Defined Under Namespace

Modules: Methods Classes: DH, DSA, EC, RSA

Class Method Summary collapse

Class Method Details

.load(key, **kwargs) ⇒ DSA, ...

Alias for parse.

Parameters:

  • key (String)

    The PEM or DER encoded key string.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for parse.

Options Hash (**kwargs):

  • :password (String, nil)

    Optional password to decrypt the key.

Returns:

See Also:

Since:

  • 1.0.0



84
85
86
# File 'lib/ronin/support/crypto/key.rb', line 84

def self.load(key,**kwargs)
  parse(key,**kwargs)
end

.load_file(path) ⇒ DSA, ...

Loads the key from the file.

Parameters:

  • path (String)

    The path to the key file.

Returns:

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



102
103
104
# File 'lib/ronin/support/crypto/key.rb', line 102

def self.load_file(path)
  parse(File.read(path))
end

.parse(key, password: nil) ⇒ OpenSSL::PKey, ...

Parses an PEM encoded key.

Parameters:

  • key (String)

    The PEM or DER encoded key string.

  • password (String, nil) (defaults to: nil)

    Optional password to decrypt the key.

Returns:

  • (OpenSSL::PKey)

    The parsed key.

  • (DSA, EC, RSA)

    The parsed key.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



51
52
53
54
55
56
57
58
59
60
61
62
63
# 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 EC PRIVATE KEY-----')
                EC
              else
                raise(ArgumentError,"cannot determine the key type for key: #{key.inspect}")
              end

  key_class.parse(key, password: password)
end