class Ronin::Support::Text::Homoglyph::Table

Loads a table of characters and their homoglyph characters.

@since 1.0.0

@api private

Attributes

homoglyphs[R]

The list of all homoglyph characters in the table.

@return [Array<String>]

table[R]

The table of ASCII characters and their homoglyph counterparts.

@return [Hash{String => Array<String>}]

Public Class Methods

load_file(path) click to toggle source

Loads a table of homoglyphs from the ‘.txt` file.

@param [String] path

The path to the `.txt` file.

@return [Table]

The newly loaded homoglyph table.

@api private

# 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
new() click to toggle source

Initializes an empty homoglyph table.

# File lib/ronin/support/text/homoglyph/table.rb, line 47
def initialize
  @homoglyphs = []
  @table      = {}
end

Public Instance Methods

+(other_table)
Alias for: merge
[](char) click to toggle source

Looks up the substitute characters for the given original character.

@param [String] char

The ASCII character to lookup in the table.

@return [Array<String>, nil]

The homoglyphic equivalent characters for the given ASCII
character.

@api public

# File lib/ronin/support/text/homoglyph/table.rb, line 89
def [](char)
  @table[char]
end
[]=(char,substitute) click to toggle source

Adds a homoglyph character for the character.

@param [String] char

The ASCII character.

@param [String] substitute

The ASCII character's homoglyph counterpart.

@return [Array<String>]

All previously added homoglyph substitution characters.

@api private

# File lib/ronin/support/text/homoglyph/table.rb, line 107
def []=(char,substitute)
  @homoglyphs << substitute
  (@table[char] ||= []) << substitute
end
each() { |char, substitute_char| ... } click to toggle source

Enumerates over all characters and their substitutions in the table.

@yield [char,substitutions]

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

@yieldparam [String] char

An ASCII character.

@yieldparam [String] substitution

A homoglyphic equivalent for the character.

@return [Enumerator]

If no block is given, an Enumerator will be returned.
# 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| ... } click to toggle source

Enumerates over every possible homoglyphic substitution of the given String.

@param [String] string

The original to perform substitutions on.

@yield [homoglyph]

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

@yieldparam [String] homoglyph

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

@return [Enumerator]

If no block is given, an Enumerator will be returned.
# 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) click to toggle source

Combines the table with another table.

@param [Table] other_table

The other table to merge together.

@return [Table]

The new merged table.
# 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
Also aliased as: +
substitute(string) click to toggle source

Performs a random homoglyphic substitution on the given String.

@param [String] string

The given String.

@return [String]

The random homoglyph string derived from the given String.

@raise [NotViable]

No homoglyph replaceable characters were found in the String.
# 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