Sha256: d4a17691f5b7bf7ab88729989d7e09e8ca1049e3cf54ac7b48bead86617c7e25

Contents?: true

Size: 1017 Bytes

Versions: 2

Compression:

Stored size: 1017 Bytes

Contents

require 'singleton'
require 'openssl'

module R509
  # a singleton class to store loaded OpenSSL Engines
  class Engine
    include Singleton

    def initialize
      @engines = {}
    end

    # @param hash Takes a hash with SO_PATH and ID
    # @return OpenSSL::Engine object
    def load(hash)
      validate_hash(hash)
      if @engines.key?(hash[:id])
        @engines[hash[:id]]
      else
        init_engine(hash)
      end
    end

    # Takes an engine ID and returns the engine object
    def [](key)
      @engines[key]
    end

    private

    def init_engine(hash)
      OpenSSL::Engine.load
      @engines[hash[:id]] = OpenSSL::Engine.by_id("dynamic") do |e|
        e.ctrl_cmd("SO_PATH", hash[:so_path])
        e.ctrl_cmd("ID", hash[:id])
        e.ctrl_cmd("LOAD")
      end
    end

    def validate_hash(hash)
      return unless !hash.respond_to?(:key?) || !hash.key?(:so_path) || !hash.key?(:id)
      raise ArgumentError, "You must supply a hash with both :so_path and :id"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
r509-1.0.1 lib/r509/engine.rb
r509-1.0 lib/r509/engine.rb