Class: FFXCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/ffxcodec.rb,
lib/ffxcodec/encoder.rb,
lib/ffxcodec/encrypt.rb,
lib/ffxcodec/version.rb

Overview

Encode / decode two integers into a single integer with optional encryption

The resulting value is a single 32 or 64-bit unsigned integer (your choice), even when encrypted.

Works by divvying up the bits of the single integer between the two component integers and running it through AES-FFX format-preserving cipher (optional).

Defined Under Namespace

Classes: Encoder, Encrypt

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(a_size, b_size) ⇒ FFXCodec

Returns a new instance of FFXCodec.

Parameters:

  • a_size (Fixnum)

    the number of bits allocated to the left integer

  • b_size (Fixnum)

    the number of bits allocated to the right integer



16
17
18
# File 'lib/ffxcodec.rb', line 16

def initialize(a_size, b_size)
  @encoder = Encoder.new(a_size, b_size)
end

Instance Method Details

#bit_lengthFixnum

Show size of the resulting integer in bits

Returns:

  • (Fixnum)

    size of the combined integer in bits



104
105
106
# File 'lib/ffxcodec.rb', line 104

def bit_length
  @encoder.size
end

#decode(c) ⇒ Array<Fixnum>

Note:

input will automatically be decrypted if encryption was setup

Decode an integer into its two component integers

Examples:

Decode unencrypted integer into component 40 and 24-bit integers

ffx = FFXCodec.new(40, 24)
ffx.decode(165828720871684)        #=> [1234567890, 4]

Decode encrypted integer into component 40 and 24-bit integers

ffx = FFXCodec.new(40, 24)
ffx.setup_encryption("2b7e151628aed2a6abf7158809cf4f3c", "9876543210")
ffx.decode(7692035069140451684)    #=> [797980150281, 5427652]

Parameters:

  • c (Fixnum, Bignum)

    value to decode

Returns:

  • (Array<Fixnum>)

    component integers



74
75
76
77
# File 'lib/ffxcodec.rb', line 74

def decode(c)
  input = @crypto ? decrypt(c) : c
  @encoder.decode(input)
end

#disable_encryptionvoid

This method returns an undefined value.

Turn off encryption



34
35
36
# File 'lib/ffxcodec.rb', line 34

def disable_encryption
  @crypto = false
end

#encode(a, b) ⇒ Fixnum, Bignum

Encode two integers into a single integer

Examples:

Encode 40 and 24-bit integers into an unencrypted 64-bit integer

ffx = FFXCodec.new(40, 24)
ffx.encode(1234567890, 4)          #=> 165828720871684

Encode 40 and 24-bit integers into an encrypted 64-bit integer

ffx = FFXCodec.new(40, 24)
ffx.setup_encryption("2b7e151628aed2a6abf7158809cf4f3c", "9876543210")
ffx.encode(797980150281, 5427652)  #=> 7692035069140451684

Parameters:

  • a (Fixnum)

    value to encode

  • b (Fixnum)

    value to encode

Returns:

  • (Fixnum, Bignum)

    encoded integer if encryption not setup

  • (Fixnum, Bignum)

    encrypted encoded integer if encryption setup



54
55
56
57
# File 'lib/ffxcodec.rb', line 54

def encode(a, b)
  c = @encoder.encode(a, b)
  @crypto ? encrypt(c) : c
end

#maximumsArray<Fixnum>

Show maximum representable base 10 value for each field

Examples:

Maximums for a 32-bit integer split into 24 and 8-bit components

ffx = FFXCodec.new(24, 8)
ffx.maximums  #=> [16777215, 255]

Maximums for a 64-bit integer split into two 32-bit components

ffx = FFXCodec.new(32, 32)
ffx.maximums  #=> [4294967295, 4294967295]

Returns:

  • (Array<Fixnum>)

    maximum representable component integers



90
91
92
# File 'lib/ffxcodec.rb', line 90

def maximums
  [@encoder.a_max, @encoder.b_max]
end

#setup_encryption(key, tweak) ⇒ void

This method returns an undefined value.

Setup encryption

Auto-enables encryption after encoding and decryption before decoding.

Parameters:

  • key (String)

    for AES as a hexadecimal string

  • tweak (String)

    for AES



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

def setup_encryption(key, tweak)
  @crypto = Encrypt.new(key, tweak, @encoder.size, 2)
end

#sizeFixnum

Show size of the resulting integer in bytes

Returns:

  • (Fixnum)

    size of the combined integer in bytes



97
98
99
# File 'lib/ffxcodec.rb', line 97

def size
  @encoder.size / 8
end