Class: FFXCodec
- Inherits:
-
Object
- Object
- FFXCodec
- 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
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Method Summary collapse
-
#bit_length ⇒ Fixnum
Show size of the resulting integer in bits.
-
#decode(c) ⇒ Array<Fixnum>
Decode an integer into its two component integers.
-
#disable_encryption ⇒ void
Turn off encryption.
-
#encode(a, b) ⇒ Fixnum, Bignum
Encode two integers into a single integer.
-
#initialize(a_size, b_size) ⇒ FFXCodec
constructor
A new instance of FFXCodec.
-
#maximums ⇒ Array<Fixnum>
Show maximum representable base 10 value for each field.
-
#setup_encryption(key, tweak) ⇒ void
Setup encryption.
-
#size ⇒ Fixnum
Show size of the resulting integer in bytes.
Constructor Details
Instance Method Details
#bit_length ⇒ Fixnum
Show size of the resulting integer in bits
104 105 106 |
# File 'lib/ffxcodec.rb', line 104 def bit_length @encoder.size end |
#decode(c) ⇒ Array<Fixnum>
input will automatically be decrypted if encryption was setup
Decode an integer into its two 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_encryption ⇒ void
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
54 55 56 57 |
# File 'lib/ffxcodec.rb', line 54 def encode(a, b) c = @encoder.encode(a, b) @crypto ? encrypt(c) : c end |
#maximums ⇒ Array<Fixnum>
Show maximum representable base 10 value for each field
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.
27 28 29 |
# File 'lib/ffxcodec.rb', line 27 def setup_encryption(key, tweak) @crypto = Encrypt.new(key, tweak, @encoder.size, 2) end |
#size ⇒ Fixnum
Show size of the resulting integer in bytes
97 98 99 |
# File 'lib/ffxcodec.rb', line 97 def size @encoder.size / 8 end |