Class: IntegerObfuscator

Inherits:
Object
  • Object
show all
Defined in:
lib/integer_obfuscator.rb,
lib/integer_obfuscator/version.rb

Overview

Obfuscates or unobfuscates 32-bit integers using a 32-bit block cipher based on SKIPJACK.

Defined Under Namespace

Classes: RangeError

Constant Summary collapse

MAX_SIZE =
(2 ** 32) - 1
VERSION =
'0.1.0'

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ IntegerObfuscator

Creates an obfuscator

Parameters:

  • key (String)

    the key used to obfuscate/unobfuscate integers



18
19
20
21
# File 'lib/integer_obfuscator.rb', line 18

def initialize(key)
  shakey = Digest::SHA1.digest(key)[0...10]
  @c = Cipher::Skip32.new(shakey)
end

Instance Method Details

#obfuscate(i) ⇒ Integer

Obfuscates an integer

Parameters:

  • i (Integer)

    the integer to obfuscate

Returns:

  • (Integer)

    the obfuscated integer

Raises:



27
28
29
30
# File 'lib/integer_obfuscator.rb', line 27

def obfuscate(i)
  require_32bit_integer(i)
  @c.encrypt([i].pack("I")).unpack("I").first
end

#unobfuscate(i) ⇒ Integer

Unobfuscates an integer

Parameters:

  • i (Integer)

    the integer to unobfuscate

Returns:

  • (Integer)

    the unobfuscated integer

Raises:



36
37
38
39
# File 'lib/integer_obfuscator.rb', line 36

def unobfuscate(i)
  require_32bit_integer(i)
  @c.decrypt([i].pack("I")).unpack("I").first
end