Module: Ronin::Support::Text::Homoglyph

Defined in:
lib/ronin/support/text/homoglyph.rb,
lib/ronin/support/text/homoglyph/table.rb,
lib/ronin/support/text/homoglyph/exceptions.rb

Overview

Since:

  • 1.0.0

Defined Under Namespace

Classes: NotViable, Table

Constant Summary collapse

DATA_DIR =

Path to the data/text/homoglyphs/ directory.

Since:

  • 1.0.0

File.expand_path(File.join(__dir__,'..','..','..','..','data','text','homoglyphs'))
ASCII =

ASCII only homoglyph rules.

Since:

  • 1.0.0

Table.load_file(File.join(DATA_DIR,'ascii.txt'))
GREEK =

Greek homoglyph rules.

Since:

  • 1.0.0

Table.load_file(File.join(DATA_DIR,'greek.txt'))
CYRILLIC =

Cyrillic homoglyph rules.

Since:

  • 1.0.0

Table.load_file(File.join(DATA_DIR,'cyrillic.txt'))
PUNCTUATION =

Punctuation/symbol homoglyph rules.

Since:

  • 1.0.0

Table.load_file(File.join(DATA_DIR,'punctuation.txt'))
LATIN_NUMBERS =

Latin numeral homoglyph rules.

Since:

  • 1.0.0

Table.load_file(File.join(DATA_DIR,'latin_numbers.txt'))
FULL_WIDTH =

Full-width homoglyph rules.

Since:

  • 1.0.0

Table.load_file(File.join(DATA_DIR,'full_width.txt'))
DEFAULT =

All homoglyph rules combined.

Since:

  • 1.0.0

GREEK + CYRILLIC + PUNCTUATION + LATIN_NUMBERS + FULL_WIDTH
CHAR_SETS =

Homoglyph rules grouped by character set.

Since:

  • 1.0.0

{
  ascii:         ASCII,
  greek:         GREEK,
  cyrillic:      CYRILLIC,
  punctuation:   PUNCTUATION,
  latin_numbers: LATIN_NUMBERS,
  full_width:    FULL_WIDTH
}

Class Method Summary collapse

Class Method Details

.each_substitution(word, char_set: nil) {|homoglyph| ... } ⇒ Enumerator

Enumerates over every homoglyph variation of the given word.

The character set to use.

Parameters:

  • word (String)

    The given word to mutate.

  • char_set (:ascii, :greek, :cyrillic, :punctuation, :latin_numbers, :full_width, nil) (defaults to: nil)

Yields:

  • (homoglyph)

    The given block will be passed each homoglyph variation of the given word.

Yield Parameters:

  • homoglyph (String)

    A variation of the given word.

Returns:

  • (Enumerator)

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

See Also:

Since:

  • 1.0.0



143
144
145
# File 'lib/ronin/support/text/homoglyph.rb', line 143

def self.each_substitution(word, char_set: nil, &block)
  self.table(char_set).each_substitution(word,&block)
end

.substitute(word, char_set: nil) ⇒ String

Returns a random homoglyph substitution of the given word.

The character set to use.

Parameters:

  • word (String)

    The given word to mutate.

  • char_set (:ascii, :greek, :cyrillic, :punctuation, :latin_numbers, :full_width, nil) (defaults to: nil)

Returns:

  • (String)

    A random homoglyphic variation of the given word.

Raises:

  • (ArgumentError)

    Could not find any matching characters to replace in the given text.

  • (NotViable)

    No homoglyph replaceable characters were found in the String.

See Also:

Since:

  • 1.0.0



115
116
117
# File 'lib/ronin/support/text/homoglyph.rb', line 115

def self.substitute(word, char_set: nil)
  self.table(char_set).substitute(word)
end

.table(char_set = nil) ⇒ Table

Looks up a homoglyph character set.

The character set name.

Parameters:

  • char_set (:ascii, :greek, :cyrillic, :punctuation, :latin_numbers, :full_width, nil) (defaults to: nil)

Returns:

  • (Table)

    The specific homoglyph character set or DEFAULT if no name was given.

Since:

  • 1.0.0



83
84
85
86
87
88
89
90
91
# File 'lib/ronin/support/text/homoglyph.rb', line 83

def self.table(char_set=nil)
  if char_set
    CHAR_SETS.fetch(char_set) do
      raise(ArgumentError,"unknown homoglyph character set (#{char_set.inspect}), must be #{CHAR_SETS.keys.map(&:inspect).join(', ')}")
    end
  else
    DEFAULT
  end
end