class BitterDomain::BitShifter

Constants

PATTERN

accept alphanumeric chars and hyphens but no hyphens at the beginning or end of the domain

Public Class Methods

new(domain) click to toggle source
# File lib/bitter_domain/bit_shifter.rb, line 7
def initialize(domain)
  @domain = domain
  @shifts = []
end

Public Instance Methods

gen_shifts(byte) click to toggle source
# File lib/bitter_domain/bit_shifter.rb, line 16
def gen_shifts(byte)
  # left-shift 1 through each bit of the byte
  # and XOR to flip a single one at each position
  8.times do |i|
    shifted = byte ^ (1 << i)
    @shifts.push(shifted)
  end

  @shifts
end
get_shifted_domains() click to toggle source
# File lib/bitter_domain/bit_shifter.rb, line 27
def get_shifted_domains
  bytes = @domain.bytes
  domains = []

  # iterate over a byte array for the original domain
  # get a set of flips for each byte, swap each new byte
  # and then stringify
  bytes.each.with_index do |byte, idx|
    shifts = gen_shifts(byte)
    swapped_domains = shifts.map do |byte| 
      copied_bytes = bytes.dup
      copied_bytes[idx] = byte
      copied_bytes
        .map(&:chr)
        .join("")
        .downcase
    end
    domains.concat(swapped_domains)
  end

  # kick out anything that doesn't match the pattern above
  domains.keep_if { |domain| valid_domain?(domain) }.uniq
end
valid_domain?(domain) click to toggle source
# File lib/bitter_domain/bit_shifter.rb, line 12
def valid_domain?(domain)
  domain.match?(PATTERN) && domain != @domain
end