Skip to main content

Shuffle bits in integer numbers

Project description

bit-permutation

Shuffle bits in integer numbers.

PyPI - Version Documentation Status codecov

Overview

The bit-permutation package provides tools for shuffling bits in integer numbers. It includes a set of classes designed to handle bit permutations and inversions.

The primary application of this module is to obscure monotonically increasing numbers, such as auto-incrementing database identifiers, which can be vulnerable to exploitation through Insecure Direct Object Reference as described by OWASP. By rearranging and inverting bits within these integer identifiers, the sequential nature of them can be made less obvious, thereby adding an additional layer of security.

While this technique is an example of security through obscurity and should not be relied upon as a substitute for comprehensive information hiding practices, it can still be valuable in various scenarios. The module enables the creation of a defined or random combination of bit permutation and inversion, resulting in a bijective transformation of a set of integers.

Disclaimer

  1. Not intended for cryptographic use: this module is not designed or intended for use in cryptography. The algorithms and functions provided do not offer the security guarantees required for cryptographic applications.

  2. Not suitable for highly loaded applications: The module is not optimized for performance in highly loaded or real-time environments. Users should avoid deploying this module in scenarios where performance and efficiency are critical. See also the Performance overview section.

  3. Not for mathematical applications: Although the module provides some functions for checking the properties of permutations, it is not intended for rigorous mathematical applications. The provided functionality may be useful for basic operations and educational purposes, but it should not be relied upon for advanced or formal studies in combinatorics or group theory.

Installation

Requires Python version 3.10 or higher. To install the package, run the following command:

pip install bit-permutation

Synopsis

from bit_permutation import BitShuffle

# Create a random permutation for lower 16 bits.
# Higher bits will be left unchanged.
bs = BitShuffle.generate_random(16)

# Sequential numbers turn into a list, for example,
# [42525, 42517, 9757, 9749, 42509, 42501, 9741, 9733, 34333, 34325]
shuffled = [bs.shuffle(x) for x in range(10)]

# Back to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
original = [bs.unshuffle(y) for y in shuffled]

# Prints 614290679212893317370896 or whatever, a number that contains 
# the permutation and inversion state and can be used 
# to restore it later with BitShuffle.unpack()
print(bs.pack())

Classes

BitPermutation

The BitPermutation class appears to handle various aspects of bit permutation, including generating random permutations, checking properties of permutation (like whether it is identity or involution), and providing different representations (cycles, tuples, Lehmer codes).

Performance overview

The module leverages basic bitwise operations such as shifts and mask applications to perform permutations, rather than employing advanced algorithms optimized for speed, like Beneš transformation network or bytes swapping. While methods are not the most optimal, they are straightforward and sufficient for many use cases.

It's important to note that Python, as an interpreted language, is generally slower compared to compiled languages. The actual speed of execution can vary depending on several factors, including the specific permutation chosen and the number of bits set in the given argument.

However, composite tests have shown that on a modern processor core (as of 2024), the module is capable of performing approximately 1 million operations per second for 16-bit numbers and 100,000 operations per second for 128-bit numbers.

License

Copyright 2024 Oleh Alistratov

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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

bit_permutation-1.0.16.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bit_permutation-1.0.16-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file bit_permutation-1.0.16.tar.gz.

File metadata

  • Download URL: bit_permutation-1.0.16.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for bit_permutation-1.0.16.tar.gz
Algorithm Hash digest
SHA256 1d3a558f53277878527d7d741f06209e2869dfd244f37f6c18639e6ee3f95595
MD5 f9ebf83147db25bb387c98dd13ddd8d1
BLAKE2b-256 4945ce9c17d073e18d89bb210e6aaa411b18a5851994d647eb5e21fbaf073c29

See more details on using hashes here.

File details

Details for the file bit_permutation-1.0.16-py3-none-any.whl.

File metadata

File hashes

Hashes for bit_permutation-1.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 95b3b4b8c78de62a5c79b950295e1aa19d44a50221f48b58cb407fa4f61c3295
MD5 6c0ccf04a9142eae120e5635e374317e
BLAKE2b-256 3fabf3ec02dfabd7ecd884182e9c3cb4bffd9a1694170c4c83b1446cd27e7aaa

See more details on using hashes here.

Supported by

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