Coder Social home page Coder Social logo

ffxcodec's Introduction

FFXCodec

Encodes two unsigned integers into a single, larger (32 or 64-bit) integer.

Optionally, it can encrypt/decrypt the resulting integer using an implementation of the AES-FFX format-preserving cipher.

Has no external dependencies. Everything is done with the stdlib.

Usage

Start by divvying up the 32 or 64 bits that will make up the resulting integer:

ffx = FFXCodec.new(16, 16)  # divide 32-bit int equally
ffx = FFXCodec.new(40, 24)  # divide 64-bit int into a 40 and 24-bit int

Then, encode and decode accordingly:

ffx.encode(1234567890, 4)          #=> 165828720871684
ffx.decode(165828720871684)        #=> [1234567890, 4]

Optionally, you can enable encryption by setting a key and tweak:

ffx.setup_encryption("2b7e151628aed2a6abf7158809cf4f3c", "9876543210")
ffx.encode(797980150281, 5427652)  #=> 7692035069140451684
ffx.decode(7692035069140451684)    #=> [797980150281, 5427652]

Putting it all together

Example without encryption (40 and 24-bit integers into 64-bit):

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

Example with encryption (40 and 24-bit integers into 64-bit):

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

Installation

To install:

gem install ffxcodec

Add this line to your application's Gemfile:

gem 'ffxcodec'

FAQ

Q. Does this only work with unsigned integers?

A. It could be made to work with signed integers, but it wasn't built or tested with that use case in mind.

Q. What is a tweak?

A. It's kind of like a salt. The initial FFX spec has a good description.

Alternatives

Encoding:

  • "Mortonizing" / Z-Order Curve

Encryption:

Warning

The AES-FFX implementation is experimental. It was cooked up for this proof of concept.

The tests included are based on the NIST reference vectors, but the published vectors only cover radix 10 and 36.

Additionally, FFX is still a DRAFT specification. Thus, it cannot yet be considered cryptographically secure.

Don't use this for anything beyond basic obfuscation.

Known Issues

  • Assumes little-endian.
  • Assumes 64-bit capable.

Author

ffxcodec's People

Contributors

brandt avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.