obfuskey-rb

A Ruby port of Obfuskey. Generates deterministic, reversible, fixed-length keys from integer values using a custom alphabet. Cross-compatible with the Python, JavaScript, and Rust implementations — the same (alphabet, key_length, value) triple produces the same key in every language.

Install

# Gemfile
gem "obfuskey"

Usage

require "obfuskey"

obf = Obfuskey.new(Obfuskey::Alphabets::BASE62)
key = obf.get_key(12345)       # => "d2Aasl"
obf.get_value(key)             # => 12345

Custom key length or multiplier

Obfuskey.new(Obfuskey::Alphabets::BASE62, key_length: 8)
Obfuskey.new(Obfuskey::Alphabets::BASE62, multiplier: 123)   # odd integer

Obfusbit (bit-packed structured keys)

schema = [
  { name: "id",   bits: 10 },
  { name: "type", bits: 2  },
  { name: "flag", bits: 1  }
]

key_maker = Obfuskey.new(Obfuskey::Alphabets::BASE62, key_length: 3)
ob = Obfusbit.new(schema, obfuskey: key_maker)

encoded = ob.pack({ "id" => 100, "type" => 2, "flag" => 1 }, obfuscate: true)
ob.unpack(encoded, obfuscated: true)
# => { "id" => 100, "type" => 2, "flag" => 1 }

Alphabets

Obfuskey::Alphabets provides BASE16, BASE32, BASE36, BASE52, BASE56, BASE58, BASE62, BASE64, BASE94, CROCKFORD_BASE32, ZBASE32, BASE64_URL_SAFE.

Tests

bundle install
bundle exec rspec

License

MIT