python-optimus
This is based fully on pjebs/optimus-go for Go which is based on jenssegers/optimus for PHP which is based on Knuth's Integer Hashing (Multiplicative Hashing) from his book The Art Of Computer Programming, Vol. 3, 2nd Edition, Section 6.4, Page 516.
With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings. It is also super fast.
This library supports both 32 and 64 bits integers, although in Python you don't have that differentiation between int32 and int64, even bigint or bignum is the same since PEP 237. The reason you need a bitlength is that the algorithm itself works on a fixed bitlength. By default this library uses 64 bits.
Python Support
So far it's only tested on Python 3.8 and Python 3.9
Installation
pip install python-optimus
Usage
Basic usage:
from optimus_ids import Optimus
my_optimus = Optimus(
prime=<your prime number>
)
my_int_id = <some id you have>
my_int_id_hashed = my_optimus.encode(my_int_id)
assert my_int_id == my_optimus.decode(my_int_id_hashed)
The caveat with the usage above is that every time you create your Optimus instance it will have a random component, even with using the same prime, so a proper usage should be like this:
from optimus_ids import Optimus
my_optimus = Optimus(
prime=<your prime number>,
random=<some random number>
)
my_int_id = <some id you have>
my_int_id_hashed = my_optimus.encode(my_int_id)
assert my_int_id == my_optimus.decode(my_int_id_hashed)
To generate a suitable random number you could do this:
from optimus_ids import rand_n, MAX_64_INT # use 32 instead of 64 if you want to
my_random_number = rand_n(MAX_64_INT - 1)
You can also generate an Optimus intance and then keep its prime, inverse and random properties stored, so you can always configure a new instance with the same components, or even pickle it:
from optimus_ids import generate, Optimus
my_optimus = generate()
# store the following variables or pickle the my_optimus variable
prime = my_optimus.prime
inverse = my_optimus.inverse
random = my_optimus.random
bitlength = my_optimus.bitlength
# create a new instance with the same parameters or unpickle an instance
my_other_optimus = Optimus(
prime=prime,
inverse=inverse,
random=random,
bitlength=bitlength,
)
assert my_optimus.encode(42) == my_other_optimus.encode(42)
assert my_optimus.decode(my_other_optimus.encode(42)) == my_other_optimus.decode(my_optimus.encode(42))
NOTE for the generate function to work, it needs data, the data is large, and not available with the package, the data should be downloaded from here and the path to it is passed to the generate function. By default it expects the data to be in a folder called optimus-primes in the current working directory.
├── your-app.py
├── ...
└── optimus-primes
├── p1.txt
├── p2.txt
├── ...
└── p50.txt
Check the tests folder for test cases and other usage examples.
License
This work is licensed under MIT License.
Metadata
Release files for python-optimus 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| python-optimus-1.0.1.tar.gz | 3.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_optimus-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.2 kB
Release files / python-optimus-1.0.1.tar.gz
| Download URL | python-optimus-1.0.1.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d517c44b08a56557982dd55ea0e5ace02394407a761a4f501e48ab77cb9d1567
|
|
BLAKE2b-256 checksum How to use checksums |
8726889eb8ba98b8db3e588bc312f176e1d325e3e89cc7daec50969a2b19e3b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
|
Release files / python_optimus-1.0.1-py3-none-any.whl
| Download URL | python_optimus-1.0.1-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
44cc8113b2edb94f1617411b69a54c71ec5687af171ee391650340977bb51036
|
|
BLAKE2b-256 checksum How to use checksums |
095fe49f5ee32ca82ee15420ec86d3f12d2a88eda5bc8ea6fc12a8a1e15cbd3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
|
Security for python-optimus 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.