Class: ScatterSwap::Hasher

Inherits:
Object
  • Object
show all
Defined in:
lib/scatter_swap/hasher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_integer, spin = 0) ⇒ Hasher

Returns a new instance of Hasher.



5
6
7
8
9
10
# File 'lib/scatter_swap/hasher.rb', line 5

def initialize(original_integer, spin = 0)
  @original_integer = original_integer
  @spin = spin
  zero_pad = original_integer.to_s.rjust(10, '0')
  @working_array = zero_pad.split("").collect {|d| d.to_i}
end

Instance Attribute Details

#working_arrayObject

Returns the value of attribute working_array.



3
4
5
# File 'lib/scatter_swap/hasher.rb', line 3

def working_array
  @working_array
end

Instance Method Details

#completed_stringObject



26
27
28
# File 'lib/scatter_swap/hasher.rb', line 26

def completed_string
  @working_array.join
end

#hashObject

obfuscates an integer up to 10 digits in length



13
14
15
16
17
# File 'lib/scatter_swap/hasher.rb', line 13

def hash
  swap
  scatter
  completed_string
end

#reverse_hashObject

de-obfuscates an integer



20
21
22
23
24
# File 'lib/scatter_swap/hasher.rb', line 20

def reverse_hash
  unscatter
  unswap
  completed_string
end

#scatterObject

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



56
57
58
59
60
61
# File 'lib/scatter_swap/hasher.rb', line 56

def scatter
  sum_of_digits = @working_array.inject(:+).to_i
  @working_array = 10.times.collect do 
    @working_array.rotate!(spin ^ sum_of_digits).pop
  end
end

#spinObject

Add some spice so that different apps can have differently mapped hashes



77
78
79
# File 'lib/scatter_swap/hasher.rb', line 77

def spin
  @spin || 0
end

#swapObject

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



40
41
42
43
44
# File 'lib/scatter_swap/hasher.rb', line 40

def swap
  @working_array = @working_array.collect.with_index do |digit, index|
    swapper_map(index)[digit]
  end
end

#swapper_map(index) ⇒ Object

We want a unique map for each place in the original number



31
32
33
34
35
36
# File 'lib/scatter_swap/hasher.rb', line 31

def swapper_map(index)
  array = (0..9).to_a
  10.times.collect.with_index do |i|
    array.rotate!(index + i ^ spin).pop
  end
end

#unscatterObject

Reverse the scatter



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/scatter_swap/hasher.rb', line 64

def unscatter
  scattered_array = @working_array
  sum_of_digits = scattered_array.inject(:+).to_i
  @working_array = []
  @working_array.tap do |unscatter| 
    10.times do
      unscatter.push scattered_array.pop
      unscatter.rotate! (sum_of_digits ^ spin) * -1
    end
  end
end

#unswapObject

Reverse swap



47
48
49
50
51
# File 'lib/scatter_swap/hasher.rb', line 47

def unswap
  @working_array = @working_array.collect.with_index do |digit, index|
    swapper_map(index).rindex(digit)
  end
end