Class: MaskedId::ScatterSwap

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_integer, spin = 0) ⇒ ScatterSwap

Returns a new instance of ScatterSwap.



62
63
64
65
66
67
# File 'lib/masked_id/scatter_swap.rb', line 62

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.



69
70
71
# File 'lib/masked_id/scatter_swap.rb', line 69

def working_array
  @working_array
end

Class Method Details

.hash(plain_integer, spin = 0) ⇒ Object

Convience class method pointing to the instance method



53
54
55
# File 'lib/masked_id/scatter_swap.rb', line 53

def self.hash(plain_integer, spin = 0)
  new(plain_integer, spin).hash
end

.reverse_hash(hashed_integer, spin = 0) ⇒ Object

Convience class method pointing to the instance method



58
59
60
# File 'lib/masked_id/scatter_swap.rb', line 58

def self.reverse_hash(hashed_integer, spin = 0)
  new(hashed_integer, spin).reverse_hash
end

Instance Method Details

#completed_stringObject



85
86
87
# File 'lib/masked_id/scatter_swap.rb', line 85

def completed_string
  @working_array.join
end

#hashObject

obfuscates an integer up to 10 digits in length



72
73
74
75
76
# File 'lib/masked_id/scatter_swap.rb', line 72

def hash
  swap
  scatter
  completed_string
end

#reverse_hashObject

de-obfuscates an integer



79
80
81
82
83
# File 'lib/masked_id/scatter_swap.rb', line 79

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



115
116
117
118
119
120
# File 'lib/masked_id/scatter_swap.rb', line 115

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



136
137
138
# File 'lib/masked_id/scatter_swap.rb', line 136

def spin
  @spin || 0
end

#swapObject

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



99
100
101
102
103
# File 'lib/masked_id/scatter_swap.rb', line 99

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



90
91
92
93
94
95
# File 'lib/masked_id/scatter_swap.rb', line 90

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



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/masked_id/scatter_swap.rb', line 123

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



106
107
108
109
110
# File 'lib/masked_id/scatter_swap.rb', line 106

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