summaryrefslogtreecommitdiff
path: root/lib/xml/kit/crypto.rb
blob: 2cfd12bc51c1d041d8e04519d683054f1b01d708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

require 'xml/kit/crypto/oaep_cipher'
require 'xml/kit/crypto/rsa_cipher'
require 'xml/kit/crypto/symmetric_cipher'
require 'xml/kit/crypto/unknown_cipher'

module Xml
  module Kit
    module Crypto
      CIPHERS = [SymmetricCipher, RsaCipher, OaepCipher, UnknownCipher].freeze

      # @!visibility private
      def self.cipher_for(algorithm, key)
        CIPHERS.find { |x| x.matches?(algorithm) }.new(algorithm, key)
      end

      def self.cipher_registry(&block)
        BlockRegistry.new(&block)
      end

      class BlockRegistry
        def initialize(&factory)
          @factory = factory
        end

        def cipher_for(algorithm, key)
          @factory.call(algorithm, key)
        end
      end
    end
  end
end