module Ronin::Support::Crypto::Key::Methods

Common methods for {Key} classes.

@api private

@since 1.0.0

Public Class Methods

included(key_class) click to toggle source

Extends {ClassMethods}.

@param [Class] key_class

The {Key} class that is including {Methods}.
# File lib/ronin/support/crypto/key/methods.rb, line 39
def self.included(key_class)
  key_class.extend ClassMethods
end

Public Instance Methods

save(path, encoding: :pem, cipher: 'aes-256-cbc', password: nil) click to toggle source

Saves the key to the given path.

@param [String] path

The path to write the exported key to.

@param [:pem, :der] encoding

The desired encoding of the exported key.
* `:pem` - PEM encoding.
* `:der` - DER encoding.

@param [String, nil] cipher

Optional cipher to use to encrypt the key file.

@param [String, nil] password

Optional password to use to encrypt the key file.

@raise [ArgumentError]

The `endcoding:` value must be either `:pem` or `:der`.
# File lib/ronin/support/crypto/key/methods.rb, line 145
def save(path, encoding: :pem, cipher: 'aes-256-cbc', password: nil)
  encoding_method = case encoding
                    when :pem then method(:to_pem)
                    when :der then method(:to_der)
                    else
                      raise(ArgumentError,"encoding: keyword argument (#{encoding.inspect}) must be either :pem or :der")
                    end

  exported = if password
               cipher = OpenSSL::Cipher.new(cipher)
               encoding_method.call(cipher,password)
             else
               encoding_method.call
             end

  File.write(path,exported)
end