Sha256: fa5e22e4bbefe43920921bb091ceb3c2f7938d0e77ad60dad6750e0534181d44

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents


module Binenc
  module Ruby
    
    class ASN1Sequence
      include ASN1Object 

      def initialize(*args, &block)
        super
        @value = to_value(@value) 
      end

      def encoded
        @value = [@value] if not @value.is_a?(Array)
        value = to_encoded(@value)
        OpenSSL::ASN1::Sequence.new(value).to_der
      end

      def is_equal?(val)
        not @value.difference(val).any?
      end

      private
      #def is_binary_string?(str)
      #  # https://stackoverflow.com/a/32536221/3625825
      #  str.count('01') == str.size
      #end

      def to_encoded(val)

        if not val.nil?
          val = [val] if not val.is_a?(Array)
          
          v = val.map do |e|

            case e
            when Integer
              ASN1Integer.new(e).encoded
            when String
              if Binenc::Ruby.is_binary_string?(e)
                ASN1Binary.new(e).encoded
              else
                ASN1String.new(e).encoded
              end
            when Time
              ASN1DateTime.new(e).encoded
            else
              logger.debug "Missed #{e} / #{e.class}"
              nil
            end

          end # map

          v.delete_if { |e| e.nil? }

        else
          []
        end


      end

      def to_value(arr)
       
        if not arr.nil? 
          arr = [arr] if not arr.is_a?(Array)

          arr.map do |e|

            case e
            when OpenSSL::ASN1::Primitive
              v = e.value

              if v.is_a?(OpenSSL::BN)
                v.to_i
              else
                v
              end
            else
              e
            end

          end # map

        else
          []
        end

      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
binenc-ruby-0.1.0 lib/binenc/asn1_engine/object/sequence.rb