Sha256: c1122e80b0c22ead1d60157bd79e6834fa6b638713753d5ba3c3ab2c5b901885

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents


module Ccrypto
  module Java
    module DataConversion
      
      def to_hex(bin)
        String.from_java_bytes(org.bouncycastle.util.encoders.Hex.encode(to_java_bytes(bin)))
      end
      # end to_hex

      def from_hex(str)
        org.bouncycastle.util.encoders.Hex.decode(str)
      end
      # end from_hex

      def to_b64(bin)
        String.from_java_bytes(java.util.Base64.encoder.encode(bin))
      end
      # end to_b64
      #

      def to_b64_mime(bin)
        String.from_java_bytes(java.util.Base64.mime_encoder.encode(bin))
      end

      def from_b64(str)
        java.util.Base64.decoder.decode(str)
      end
      # end from_b64

      def to_str(bin)
        if bin.is_a?(::Java::byte[])
          String.from_java_bytes(bin)
        else
          bin
        end
      end

      def to_bin(str)
        if str.nil?
          ::Java::byte[0].new
        else
          str.to_java.getBytes
        end
      end

      def to_java_bytes(val, encoding = nil)
        case val
        when String
          val.to_java_bytes
        when java.lang.String
          if not_empty?(encoding)
            val.getBytes(encoding)
          else
            val.getBytes
          end
        when Ccrypto::Java::ManagedMemoryBuffer
          val.bytes
        else
          if val.respond_to?(:encoded)
            val.encoded
          else
            val
          end
        end
      end

      # 
      # Add the methods to class level
      #
      def self.included(klass)
        klass.class_eval <<-END
          extend Ccrypto::Java::DataConversion
        END
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ccrypto-java-0.1.0 lib/ccrypto/java/data_conversion.rb