Sha256: 22ee6551bf3b33c1d0d9492b84dd88f7e515b3bd8d8ff4b9221824125e6f1594

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# used for encrypting and decrypting data in forms

module Lux
  class Current
    module EncryptParams
      extend self

      # encrypt_param('dux', 'foo')
      # <OpenStruct name="_data_1", value="eyJ0eXAiOiJKV1QiLCJhbGciOi..."
      def encrypt name, value
        base = name.include?('[') ? name.split(/[\[\]]/).first(2).join('::') : name
        base += '#%s' % value

        OpenStruct.new(name: "_data_#{Lux.current.uid}", value: Crypt.encrypt(base))
      end

      def hidden_input name, value
        data = encrypt name, value

        %[<input type="hidden" name="#{data.name}" value="#{data.value}" />]
      end

      # decrypts params starting with _data_
      def decrypt hash
        for key in hash.keys
          next unless key.starts_with?('_data_')
          data = Crypt.decrypt(hash.delete(key))
          data, value = data.split('#', 2)
          data = data.split('::')

          if data[1]
            hash[data[0]] ||= {}
            hash[data[0]][data[1]] = value
          else
            hash[data[0]] = value
          end
        end

        hash
      rescue
        Lux.log ' Lux::Current::EncryptParams decrypt error'.red
        {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lux-fw-0.6.2 ./lib/lux/current/lib/encrypt_params.rb