Sha256: 4ec231cf31aa80b61b980b07ad12255511a5df71b63c67df43399864c93a0f74

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# used for encrypting and decrypting data in forms

module Lux::Page::EncryptParams
  extend self

  @cnt = 0

  # 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_#{@cnt+=1}", 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::Page::EncryptParams decrypt error'.red
    {}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.1.35 ./lib/lux/page/lib/encrypt_params.rb
lux-fw-0.1.17 ./lib/lux/page/lib/encrypt_params.rb