kpermute

kpermute

Used in: klause.

Kpermute turns a contiguous range of integers into a pseudo-random permutation and back, so a counter 0, 1, 2, ... becomes a sequence that looks shuffled but is fully reversible from a key. Two primary use cases. Generating non-sequential public IDs without a lookup table, where a database primary key stays a dense integer internally while the externally visible form reveals nothing about insertion order or row count. Lazily shuffling huge lists, where wrapping an indexed list with the permutation gives you a shuffled view without materialising the shuffle, even for ranges that wouldn’t fit in memory.

Permutations work over arbitrary integer ranges, including negatives and the full 32- or 64-bit signed domain. Encoding and decoding run in constant memory using a keyed xor-shift-multiply mixer with cycle-walking, never a lookup table, so the same call cost holds whether the range has a thousand elements or a trillion. Not a cryptographic primitive: the mixer is built for speed and reversibility, not adversarial resistance, so don’t lean on it where an attacker shouldn’t be able to recover the underlying counter.

Install

dependencies {
  implementation("com.eignex:kpermute:1.2.0")
}

Where to go next