class FFXCodec
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).
Constants
- VERSION
Public Class Methods
@param [Fixnum] a_size the number of bits allocated to the left integer @param [Fixnum] b_size the number of bits allocated to the right integer
# File lib/ffxcodec.rb, line 16 def initialize(a_size, b_size) @encoder = Encoder.new(a_size, b_size) end
Public Instance Methods
Show size of the resulting integer in bits
@return [Fixnum] size of the combined integer in bits
# File lib/ffxcodec.rb, line 104 def bit_length @encoder.size end
Decode an integer into its two component integers
@note input will automatically be decrypted if encryption was setup @param [Fixnum, Bignum] c value to decode
@example Decode unencrypted integer into component 40 and 24-bit integers
ffx = FFXCodec.new(40, 24) ffx.decode(165828720871684) #=> [1234567890, 4]
@example 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]
@return [Array<Fixnum>] component integers
# File lib/ffxcodec.rb, line 74 def decode(c) input = @crypto ? decrypt(c) : c @encoder.decode(input) end
Turn off encryption
@return [void]
# File lib/ffxcodec.rb, line 34 def disable_encryption @crypto = false end
Encode two integers into a single integer
@param [Fixnum] a value to encode @param [Fixnum] b value to encode
@example Encode 40 and 24-bit integers into an unencrypted 64-bit integer
ffx = FFXCodec.new(40, 24) ffx.encode(1234567890, 4) #=> 165828720871684
@example 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
@return [Fixnum, Bignum] encoded integer if encryption not setup @return [Fixnum, Bignum] encrypted encoded integer if encryption setup
# File lib/ffxcodec.rb, line 54 def encode(a, b) c = @encoder.encode(a, b) @crypto ? encrypt(c) : c end
Show maximum representable base 10 value for each field
@example Maximums for a 32-bit integer split into 24 and 8-bit components
ffx = FFXCodec.new(24, 8) ffx.maximums #=> [16777215, 255]
@example Maximums for a 64-bit integer split into two 32-bit components
ffx = FFXCodec.new(32, 32) ffx.maximums #=> [4294967295, 4294967295]
@return [Array<Fixnum>] maximum representable component integers
# File lib/ffxcodec.rb, line 90 def maximums [@encoder.a_max, @encoder.b_max] end
Setup encryption
Auto-enables encryption after encoding and decryption before decoding.
@param [String] key for AES as a hexadecimal string @param [String] tweak for AES @return [void]
# File lib/ffxcodec.rb, line 27 def setup_encryption(key, tweak) @crypto = Encrypt.new(key, tweak, @encoder.size, 2) end
Show size of the resulting integer in bytes
@return [Fixnum] size of the combined integer in bytes
# File lib/ffxcodec.rb, line 97 def size @encoder.size / 8 end
Private Instance Methods
@param [Fixnum, Bignum] value to decrypt
# File lib/ffxcodec.rb, line 116 def decrypt(value) @crypto.decrypt(value.to_s(2)).to_i(2) end
@param [Fixnum, Bignum] value to encrypt
# File lib/ffxcodec.rb, line 111 def encrypt(value) @crypto.encrypt(value.to_s(2)).to_i(2) end