Skip to main content

Pseudo-random permutation of 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

This library is also implemented in Ruby and Go.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

intperm-1.1.0.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file intperm-1.1.0.tar.gz.

File metadata

  • Download URL: intperm-1.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for intperm-1.1.0.tar.gz
Algorithm Hash digest
SHA256 953814346e11d05ffed654d5b1ff0d5e61e3d40af988b0fe6ad9b0ddeaf826fc
MD5 a34d9942485b00786d4e71c59c80a64d
BLAKE2b-256 800541858ac8a3970ca8c69edb2bd08fe5dc8e4831afe7f88ba3c31b5932eaa5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page