|
| 1 | +# python-optimus |
| 2 | +This is based fully on [pjebs/optimus-go](https://github.com/pjebs/optimus-go) for Go which is based on [jenssegers/optimus](https://github.com/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](https://archive.org/details/B-001-001-250/page/n535/mode/2up), Section 6.4, Page 516. |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | +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](https://www.python.org/dev/peps/pep-0237/). The reason you need a bitlength is that the algorithm itself works on a fixed bitlength. By default this library uses 64 bits. |
| 7 | + |
| 8 | +## Python Support |
| 9 | + |
| 10 | +So far it's only tested on Python 3.8 and Python 3.9 |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | + pip install python-optimus |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Basic usage: |
| 19 | + |
| 20 | +``` |
| 21 | +from optimus_ids import Optimus |
| 22 | +my_optimus = Optimus( |
| 23 | + prime=<your prime number> |
| 24 | +) |
| 25 | +my_int_id = <some id you have> |
| 26 | +my_int_id_hashed = my_optimus.encode(my_int_id) |
| 27 | +assert my_int_id == my_optimus.decode(my_int_id_hashed) |
| 28 | +``` |
| 29 | + |
| 30 | +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: |
| 31 | + |
| 32 | +``` |
| 33 | +from optimus_ids import Optimus |
| 34 | +my_optimus = Optimus( |
| 35 | + prime=<your prime number>, |
| 36 | + random=<some random number> |
| 37 | +) |
| 38 | +my_int_id = <some id you have> |
| 39 | +my_int_id_hashed = my_optimus.encode(my_int_id) |
| 40 | +assert my_int_id == my_optimus.decode(my_int_id_hashed) |
| 41 | +
|
| 42 | +To generate a random number you could do this: |
| 43 | +``` |
| 44 | + |
| 45 | +``` |
| 46 | +from optimus_ids import rand_n, MAX_64_INT # use 32 instead of 64 if you want to |
| 47 | +my_random_number = rand_n(MAX_64_INT - 1) |
| 48 | +``` |
| 49 | + |
| 50 | +You can also generate an `Optimus` intance and then keep its `prime`, `inverese` and `random` properties stored, so you can always configure a new instance with the same components, or even pickle it: |
| 51 | + |
| 52 | +``` |
| 53 | +from optimus_ids import generate, Optimus |
| 54 | +my_optimus = generate() |
| 55 | +
|
| 56 | +# store the following variables or pickle the my_optimus variable |
| 57 | +prime = my_optimus.prime |
| 58 | +inverse = my_optimus.inverse |
| 59 | +random = my_optimus.random |
| 60 | +bitlength = my_optimus.bitlength |
| 61 | +
|
| 62 | +# create a new instance with the same parameters or unpickle an instance |
| 63 | +my_other_optimus = Optimus( |
| 64 | + prime=prime, |
| 65 | + inverse=inverse, |
| 66 | + random=random, |
| 67 | + bitlength=bitlength, |
| 68 | +) |
| 69 | +assert my_optimus.encode(42) == my_other_optimus.encode(42) |
| 70 | +assert my_optimus.decode(my_other_optimus.encode(42)) == my_other_optimus.decode(my_optimus.encode(42)) |
| 71 | +``` |
| 72 | + |
| 73 | +**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](https://github.com/pjebs/optimus-go-primes) 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. |
| 74 | + |
| 75 | +``` |
| 76 | +├── your-app.py |
| 77 | +├── ... |
| 78 | +└── optimus-primes |
| 79 | + ├── p1.txt |
| 80 | + ├── p2.txt |
| 81 | + ├── ... |
| 82 | + └── p50.txt |
| 83 | +``` |
0 commit comments