Class: Ronin::Support::Text::Homoglyph::Table Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/text/homoglyph/table.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Loads a table of characters and their homoglyph characters.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes an empty homoglyph table.

Since:

  • 1.0.0



47
48
49
50
# File 'lib/ronin/support/text/homoglyph/table.rb', line 47

def initialize
  @homoglyphs = []
  @table      = {}
end

Instance Attribute Details

#homoglyphsArray<String> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The list of all homoglyph characters in the table.

Returns:

Since:

  • 1.0.0



37
38
39
# File 'lib/ronin/support/text/homoglyph/table.rb', line 37

def homoglyphs
  @homoglyphs
end

#tableHash{String => Array<String>} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The table of ASCII characters and their homoglyph counterparts.

Returns:

Since:

  • 1.0.0



42
43
44
# File 'lib/ronin/support/text/homoglyph/table.rb', line 42

def table
  @table
end

Class Method Details

.load_file(path) ⇒ Table

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads a table of homoglyphs from the .txt file.

Parameters:

  • path (String)

    The path to the .txt file.

Returns:

  • (Table)

    The newly loaded homoglyph table.

Since:

  • 1.0.0



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ronin/support/text/homoglyph/table.rb', line 63

def self.load_file(path)
  table = new

  File.open(path) do |file|
    file.each_line(chomp: true) do |line|
      char, substitute = line.split(' ',2)

      table[char] = substitute
    end
  end

  return table
end

Instance Method Details

#[](char) ⇒ Array<String>?

Looks up the substitute characters for the given original character.

Parameters:

  • char (String)

    The ASCII character to lookup in the table.

Returns:

  • (Array<String>, nil)

    The homoglyphic equivalent characters for the given ASCII character.

Since:

  • 1.0.0



89
90
91
# File 'lib/ronin/support/text/homoglyph/table.rb', line 89

def [](char)
  @table[char]
end

#[]=(char, substitute) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a homoglyph character for the character.

Parameters:

  • char (String)

    The ASCII character.

  • substitute (String)

    The ASCII character's homoglyph counterpart.

Returns:

  • (Array<String>)

    All previously added homoglyph substitution characters.

Since:

  • 1.0.0



107
108
109
110
# File 'lib/ronin/support/text/homoglyph/table.rb', line 107

def []=(char,substitute)
  @homoglyphs << substitute
  (@table[char] ||= []) << substitute
end

#each {|char, substitutions| ... } ⇒ Enumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enumerates over all characters and their substitutions in the table.

Yields:

  • (char, substitutions)

    If a block is given, it will be passed each ASCII character and a homoglyphic equivalent character from the table.

Yield Parameters:

  • char (String)

    An ASCII character.

  • substitution (String)

    A homoglyphic equivalent for the character.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 1.0.0



128
129
130
131
132
133
134
135
136
# File 'lib/ronin/support/text/homoglyph/table.rb', line 128

def each(&block)
  return enum_for(__method__) unless block

  @table.each do |char,substitutions|
    substitutions.each do |substitute_char|
      yield char, substitute_char
    end
  end
end

#each_substitution(string) {|homoglyph| ... } ⇒ Enumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enumerates over every possible homoglyphic substitution of the given String.

Parameters:

  • string (String)

    The original to perform substitutions on.

Yields:

  • (homoglyph)

    If a block is given, it will be passed each homoglyphic substitution of the given String.

Yield Parameters:

  • homoglyph (String)

    A copy of the given String with one character replaced with it's homoglyph equivalent from the table.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 1.0.0



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ronin/support/text/homoglyph/table.rb', line 206

def each_substitution(string)
  return enum_for(__method__,string) unless block_given?

  (string.chars & @table.keys).each do |replaceable_char|
    @table[replaceable_char].each do |substitute_char|
      offset = 0

      while (index = string.index(replaceable_char,offset))
        homoglyph        = string.dup
        homoglyph[index] = substitute_char

        yield homoglyph
        offset = index + 1
      end
    end
  end
end

#merge(other_table) ⇒ Table Also known as: +

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Combines the table with another table.

Parameters:

  • other_table (Table)

    The other table to merge together.

Returns:

  • (Table)

    The new merged table.

Since:

  • 1.0.0



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ronin/support/text/homoglyph/table.rb', line 147

def merge(other_table)
  new_table = self.class.new

  each do |char,substitute|
    new_table[char] = substitute
  end

  other_table.each do |char,other_substitute|
    new_table[char] = other_substitute
  end

  return new_table
end

#substitute(string) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Performs a random homoglyphic substitution on the given String.

Parameters:

  • string (String)

    The given String.

Returns:

  • (String)

    The random homoglyph string derived from the given String.

Raises:

  • (NotViable)

    No homoglyph replaceable characters were found in the String.

Since:

  • 1.0.0



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ronin/support/text/homoglyph/table.rb', line 175

def substitute(string)
  replaceable_chars = string.chars & @table.keys

  if replaceable_chars.empty?
    raise(NotViable,"no homoglyph replaceable characters found in String (#{string.inspect})")
  end

  replaceable_char = replaceable_chars.sample
  substitute_char  = @table[replaceable_char].sample

  return string.sub(replaceable_char,substitute_char)
end