class ScatterSwap::Hasher

Constants

DIGITS

Attributes

working_array[RW]

Public Class Methods

new(original_integer, spin = 0, length = 10) click to toggle source
# File lib/scatter_swap/hasher.rb, line 11
def initialize(original_integer, spin = 0, length = 10)
  @original_integer = original_integer
  @spin = spin
  @length = length
  zero_pad = original_integer.to_s.rjust(length, '0')
  @working_array = zero_pad.chars.map(&:to_i)
end

Public Instance Methods

hash() click to toggle source

obfuscates an integer up to @length digits in length

# File lib/scatter_swap/hasher.rb, line 20
def hash
  swap
  scatter
  @working_array.join
end
reverse_hash() click to toggle source

de-obfuscates an integer

# File lib/scatter_swap/hasher.rb, line 27
def reverse_hash
  unscatter
  unswap
  @working_array.join
end
scatter() click to toggle source

Rearrange the order of each digit in a reversible way by using the sum of the digits (which doesn't change regardless of order) as a key to record how they were scattered

# File lib/scatter_swap/hasher.rb, line 55
def scatter
  sum_of_digits = @working_array.inject(:+).to_i
  @working_array = @length.times.map do
    @working_array.rotate!(@spin ^ sum_of_digits).pop
  end
end
swap() click to toggle source

Using a unique map for each of the ten places, we swap out one number for another

# File lib/scatter_swap/hasher.rb, line 39
def swap
  @working_array = @working_array.map.with_index do |digit, index|
    swapper_map(index)[digit]
  end
end
swapper_map(index) click to toggle source
# File lib/scatter_swap/hasher.rb, line 33
def swapper_map(index)
  Swapper.instance(@spin).generate(index)
end
unscatter() click to toggle source

Reverse the scatter

# File lib/scatter_swap/hasher.rb, line 63
def unscatter
  scattered_array = @working_array
  sum_of_digits = scattered_array.inject(:+).to_i
  @working_array = []
  @working_array.tap do |unscatter|
    @length.times do
      unscatter.push scattered_array.pop
      unscatter.rotate! (sum_of_digits ^ @spin) * -1
    end
  end
end
unswap() click to toggle source

Reverse swap

# File lib/scatter_swap/hasher.rb, line 46
def unswap
  @working_array = @working_array.map.with_index do |digit, index|
    swapper_map(index).rindex(digit)
  end
end