A small library for creating reversible, obfuscated, identifier hashes using a given alphabet.
Project description
ObfusKey
ObfusKey is a utility for generating obfuscated keys of integer values. While working to modernize its predecessor, BaseHash, it was found that a lot of simplifications could be made, thus ObfusKey was born.
ObfusKey was built solely for Python 3.6 or higher. For lower versions, you can still use BaseHash.
ObfusKey will generate obfuscated, reversible keys using a given alphabet and key
length. The maximum value it can process is base ** key_length - 1
, where base
is
the length of the provided alphabet. An optional modifier can also be provided, which is
then required when reversing the key into it's original value. If a modifier is not
provided, ObfusKey will generate the next prime integer after
base ** key_length - 1
along with a prime modifier. The default prime modifier will
generate golden ratio primes, but this can be overwritten.
Install
$ pip install obfuskey
If you're building from source
$ pip install .
OR
$ poetry install
Usage
To use ObfusKey, you can use one of the available alphabets, or provide your own. You can also provide your own multiplier, or leave it blank to use the built-in prime generator.
from obfuskey import ObfusKey, alphabets
obfuscator = ObfusKey(alphabets.BASE36, key_length=8)
key = obfuscator.to_key(1234567890) # FWQ8H52I
value = obfuscator.to_value('FWQ8H52I') # 1234567890
To provide a custom multiplier, or if you to provide the prime generated from a
previous instance, you can pass it in with multiplier=
. This value has to be an odd
integer.
from obfuskey import ObfusKey, alphabets
obfuscator = ObfusKey(alphabets.BASE62, multiplier=46485)
key = obfuscator.to_key(12345) # 0cpqVJ
If you wish to generate a prime not within the golden prime set, you can overwrite the multiplier with
from obfuskey import ObfusKey, alphabets
obfuscator = ObfusKey(alphabets.BASE62, key_length=3)
key = obfuscator.to_key(123) # 1O9
obfuscator.set_prime_multiplier(1.75)
key = obfuscator.to_key(123) # Fyl
Extras
If you need to obfuscate integers that are larger than 512-bit, you will need to also have gmp2 installed.
$ pip install gmpy2
OR
poetry install -E gmpy2
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.