Class: Unisec::Confusables

Inherits:
Object
  • Object
show all
Defined in:
lib/unisec/confusables.rb

Overview

Operations about Unicode confusable characters (homoglyphs).

Class Method Summary collapse

Class Method Details

.list(chr, map: true) ⇒ Array<String>

List confusables characters for a given character

Examples:

Unisec::Confusables.list('!') # => ["!", "ǃ", "ⵑ", "‼", "⁉", "⁈"]
Unisec::Confusables.list('!', map: false) # => ["!", "ǃ", "ⵑ"]

Parameters:

  • chr (String)

    the character to search confusables for

  • map (Boolean) (defaults to: true)

    allows partial mapping, includes confusable where the given chart is a part of

Returns:

  • (Array<String>)

    list of confusables



16
17
18
# File 'lib/unisec/confusables.rb', line 16

def self.list(chr, map: true)
  Unicode::Confusable.list(chr, map)
end

.list_display(chr, map: true) ⇒ Object

Display a CLI-friendly output listing all confusables corresponding to a character (code point)

Parameters:

  • chr (String)

    the character to search confusables for

  • map (Boolean) (defaults to: true)

    allows partial mapping, includes confusable where the given chart is a part of



23
24
25
26
27
28
29
# File 'lib/unisec/confusables.rb', line 23

def self.list_display(chr, map: true)
  Confusables.list(chr, map: map).each do |confu|
    puts "#{Properties.char2codepoint(confu).ljust(9)} #{confu.ljust(4)} " \
         "#{TwitterCldr::Shared::CodePoint.get(confu.codepoints.first).name}"
  end
  nil
end

.randomize(str) ⇒ String

Replace all characters with random confusables when possible.

Examples:

Unisec::Confusables.randomize('noraj') # => "𝓃ⲟ𝓇𝒶j"
Unisec::Confusables.randomize('noraj') # => "𝗻૦𝚛⍺𝐣"
Unisec::Confusables.randomize('noraj') # => "𝔫𞺄𝕣⍺j"

Parameters:

  • str (String)

    Unicode string

Returns:

  • (String)

    input randomized with confusables



38
39
40
41
42
43
44
45
# File 'lib/unisec/confusables.rb', line 38

def self.randomize(str)
  out = ''
  str.each_char do |chr|
    confu = Confusables.list(chr, map: false).sample
    out += confu.nil? ? chr : confu
  end
  out
end

.randomize_display(str) ⇒ Object

Display a CLI-friendly output of a string where characters are replaces with random confusables

Parameters:

  • str (String)

    Unicode string



49
50
51
52
53
# File 'lib/unisec/confusables.rb', line 49

def self.randomize_display(str)
  display = ->(key, value) { puts Paint[key, :red, :bold].ljust(23) + " #{value}" }
  display.call('Original:', str)
  display.call('Transformed:', Unisec::Confusables.randomize(str))
end