Class: Ronin::Support::Text::Homoglyph::Table Private
- Inherits:
-
Object
- Object
- Ronin::Support::Text::Homoglyph::Table
- 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.
Instance Attribute Summary collapse
-
#homoglyphs ⇒ Array<String>
readonly
private
The list of all homoglyph characters in the table.
-
#table ⇒ Hash{String => Array<String>}
readonly
private
The table of ASCII characters and their homoglyph counterparts.
Class Method Summary collapse
-
.load_file(path) ⇒ Table
private
Loads a table of homoglyphs from the
.txt
file.
Instance Method Summary collapse
-
#[](char) ⇒ Array<String>?
Looks up the substitute characters for the given original character.
-
#[]=(char, substitute) ⇒ Array<String>
private
Adds a homoglyph character for the character.
-
#each {|char, substitutions| ... } ⇒ Enumerator
private
Enumerates over all characters and their substitutions in the table.
-
#each_substitution(string) {|homoglyph| ... } ⇒ Enumerator
private
Enumerates over every possible homoglyphic substitution of the given String.
-
#initialize ⇒ Table
constructor
private
Initializes an empty homoglyph table.
-
#merge(other_table) ⇒ Table
(also: #+)
private
Combines the table with another table.
-
#substitute(string) ⇒ String
private
Performs a random homoglyphic substitution on the given String.
Constructor Details
#initialize ⇒ 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.
Initializes an empty homoglyph table.
47 48 49 50 |
# File 'lib/ronin/support/text/homoglyph/table.rb', line 47 def initialize @homoglyphs = [] @table = {} end |
Instance Attribute Details
#homoglyphs ⇒ 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 list of all homoglyph characters in the table.
37 38 39 |
# File 'lib/ronin/support/text/homoglyph/table.rb', line 37 def homoglyphs @homoglyphs end |
#table ⇒ Hash{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.
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.
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.
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.
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.
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.
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.
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.
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 |