Module Encoder
In: extensions/encoder/Encoder.rb

Methods

Public Instance methods

[Source]

# File extensions/encoder/Encoder.rb, line 210
  def encodeAll(mystring)
    result = []
    result << "Hex:\t\t#{toHex(mystring)}"
    result << "URI:\t\t#{toURI(mystring)}"
    result << "UTF-7:\t\t#{toUTF7(mystring)}"
    result << "Double Percent:\t#{toDoublePercentHex(mystring)}"
    result << "Double Nibble:\t#{toDoubleNibbleHex(mystring)}"
    result << "First Nibble:\t#{toFirstNibbleHex(mystring)}"
    result << "Second Nibble:\t#{toSecondNibbleHex(mystring)}"
    result << "Percent U:\t#{toPercentU(mystring)}"
    result << "Binary:\t\t#{toBinary(mystring)}"
    result << "MD5:\t\t#{toMd5(mystring)}"
    result << "Double MD5:\t#{toMd5(toMd5(mystring))}"
    result << "SHA1:\t\t#{toSHA1(mystring)}"
    result << "DoubleSHA1:\t#{toSHA1(toSHA1(mystring))}"
    result << "Base64:\t\t#{toBase64(mystring)}"
    return result
  end

[Source]

# File extensions/encoder/Encoder.rb, line 206
  def toBase64(string)
    return Base64.encode64(string)
  end

binary

[Source]

# File extensions/encoder/Encoder.rb, line 29
  def toBinary(string)

    binary = ""
    string.scan(/./) do |char|
      binary+=char.unpack("B8").to_s
    end

    return binary.rstrip

  end

[Source]

# File extensions/encoder/Encoder.rb, line 194
  def toDoubleMd5(string)
    return Digest::MD5.hexdigest(Digest::MD5.hexdigest(string))
  end

double nibble hex encoding

[Source]

# File extensions/encoder/Encoder.rb, line 134
  def toDoubleNibbleHex(string)

    hex = ""
    string.scan(/./) do |char|
      strChar = char[0].chr.to_s.unpack("H2")
      hex+="%"
      hex+="%#{strChar[0][0].chr.unpack("H2")}"
      hex+="%#{strChar[0][1].chr.unpack("H2")}"
    end

    return hex.rstrip

  end

double percent hex encoding

[Source]

# File extensions/encoder/Encoder.rb, line 57
  def toDoublePercentHex(string)

    hex = ""
    string.scan(/./) do |char|
      hex+="%25#{char.unpack("H4").to_s}"
    end

    return hex.rstrip

  end

[Source]

# File extensions/encoder/Encoder.rb, line 202
  def toDoubleSHA1(string)
    return Digest::SHA1.hexdigest(Digest::SHA1.hexdigest(string))
  end

first nibble hex encoding

[Source]

# File extensions/encoder/Encoder.rb, line 149
  def toFirstNibbleHex(string)

    hex = ""
    string.scan(/./) do |char|
      strChar = char[0].chr.to_s.unpack("H2")[0]
      hex+="%"
      hex+="%#{strChar[0].chr.unpack("H2")}"
      hex+="#{strChar[1].chr.to_s}"
    end

    return hex.rstrip

  end

hex encoding

[Source]

# File extensions/encoder/Encoder.rb, line 45
  def toHex(string)

    hex = ""
    string.scan(/./) do |char|
      hex+="%#{char.unpack("H4").to_s}"
    end

    return hex.rstrip

  end

[Source]

# File extensions/encoder/Encoder.rb, line 190
  def toMd5(string)
    return Digest::MD5.hexdigest(string)
  end

Microsoft percent U encoding

[Source]

# File extensions/encoder/Encoder.rb, line 179
  def toPercentU(string)

    hex = ""
    string.scan(/./) do |char|
      hex+="%U00#{char.unpack("H4").to_s}"
    end

    return hex.rstrip

  end

[Source]

# File extensions/encoder/Encoder.rb, line 198
  def toSHA1(string)
    return Digest::SHA1.hexdigest(string)
  end

second nibble hex encoding

[Source]

# File extensions/encoder/Encoder.rb, line 164
  def toSecondNibbleHex(string)

    hex = ""
    string.scan(/./) do |char|
      strChar = char[0].chr.to_s.unpack("H2")[0]
      hex+="%"
      hex+="#{strChar[0].chr.to_s}"
      hex+="%#{strChar[1].chr.unpack("H2")}"
    end

    return hex.rstrip

  end

[Source]

# File extensions/encoder/Encoder.rb, line 40
  def toURI(string)
    URI.escape(string)
  end

[Source]

# File extensions/encoder/Encoder.rb, line 121
  def toUTF7(string)

    endstring = ""
    string.split(//).each do |char|
      endstring.concat(toUTF7char(char))
    end

    return endstring

  end

converts a single char into UTF-7

TODO: needs fully testing - this was all a bit of a hack

@ = +AEA- < = +ADw- & = +ACY-

[Source]

# File extensions/encoder/Encoder.rb, line 78
  def toUTF7char(char)
    s_bin8 = toBinary(char)
    s_bin16 = s_bin8.rjust(16,'0')
    a_bin6 = s_bin16.scan(/.{6}/)

    # calculate how many string elements are not in the array
    missing = ((s_bin16.length) - (a_bin6.length * 6))
    a_bin6.push s_bin16[(s_bin16.length-missing),(s_bin16.length)]

    # ensure that the last elements in the array are padded out with 0's to make 6 chars
    a_bin6[a_bin6.length-1] = a_bin6[a_bin6.length-1].ljust(6,'0')

    mainstring = ""

    a_bin6.each do |element|
      tmparr = [element]

      # TODO: figure out what is going on here
      # really i only need to encode certain characters - it may be better to just do a lookup
      # assuming it is not nothing then base64 encode
      if element.to_s != '000000'
        mainstring.concat(toBase64(tmparr.pack("B6")))
      end
    end

    puts mainstring.inspect

    mainstring.gsub!("A==\n","")

    puts a_bin6.inspect
    puts "a_bin6.length = #{a_bin6.length}"

    if a_bin6[0] == '000000'
      mainstring = "A"+mainstring
    end
    if a_bin6[a_bin6.length-1] == '000000'
      mainstring.concat('A')
    end

    return "+"+mainstring+"-"
  end

[Validate]