A simple permutation for arbitrary size integers.
Project description
This package implements a simple, configurable permutation on the set of 64-bit integers.
The permutation is based on a bitmask that maps each bit of the input to a bit of the output. The bitmask is expanded from a random seed using a PRNG, as described by George Marsaglia in his paper called Xorshift RNGs. The permutations are thus believed to be unpredictable, provided provided that the seed is kept secret.
Usage
Create a new Permutation instance by passing in an optional seed.
>>> fromo intperm import Permutation
>>> perm = Permutation(42)
>>> perm.map_to(37)
13750393542137160527L
>>> perm.map_from(13750393542137160527)
37
Not providing a seed will create a random permutation:
>>> perm = Permutation()
>>> perm.map_from(perm.map_to(37)) == 37
True
Use cases
Use cases may vary, but an example that I find useful is generating hard-to-guess, random-looking tokens based on IDs stored in a database. The IDs can be used together with the seed to decode the original ID, but their cardinality is the same as that of the IDs themselves. When used smartly, this can save you from having to index those tokens in the database.
Another good example is randomising IDs of private objects that are available via some sort of an API. Let’s say the user accounts on your website are accessible via the path /user/:id, where :id is the user’s ID. Someone could track the growth of your user base just by enumerating the URLs and keeping track of the status codes (e.g. 403 vs. 404).
Using this simple permutation, user IDs can be kept unpredictable, rendering these kinds of attacks practically useless.
See also
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file intperm-1.1.1.tar.gz
.
File metadata
- Download URL: intperm-1.1.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b83697faea4c329ca7c49220f62ce0dd0630b33b510bb4fc9eb66dda54f128e |
|
MD5 | 1271c7aaba133dd2c8a4541fa31eb06b |
|
BLAKE2b-256 | c6265253912adf5aa8bad2a0a23f4552231a19d19ac98b8ad99c88184917ebe6 |