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(integer, spin = 0, length = 10) ⇒ Hasher

Construct a new Hasher.

Parameters:

  • integer (Fixnum)

    the numeric value to be hashed/reversed.

  • spin (Fixnum) (defaults to: 0)

    a seed value.

  • length (Fixnum) (defaults to: 10)

    the number of digits in the computed hashes.



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

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

Instance Attribute Details

#lengthFixnum (readonly)

Returns the number of digits in the computed hashes.

Returns:

  • (Fixnum)

    the number of digits in the computed hashes.



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

def length
  @length
end

#spinFixnum (readonly)

Returns A seed value to add some spice, so that different apps can have differently mapped hashes.

Returns:

  • (Fixnum)

    A seed value to add some spice, so that different apps can have differently mapped hashes.



11
12
13
# File 'lib/scatter_swap/hasher.rb', line 11

def spin
  @spin
end

#working_arrayArray[Fixnum] (readonly)

Returns:

  • (Array[Fixnum])


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

def working_array
  @working_array
end

Instance Method Details

#hashString

Obfuscate the original integer value to a zero-padded String of #length digits.

Returns:

  • (String)

    the obfuscated hash.



29
30
31
32
33
# File 'lib/scatter_swap/hasher.rb', line 29

def hash
  swap
  scatter
  completed_string
end

#reverse_hashString

De-obfuscate the original integer value to a zero-padded String of #length digits.

Returns:

  • (String)

    the de-obfuscated hash.



38
39
40
41
42
# File 'lib/scatter_swap/hasher.rb', line 38

def reverse_hash
  unscatter
  unswap
  completed_string
end