Skip to main content

Reversible obfuscated identifier hashes.

Project description

BaseHash
========

BaseHash is a small library for creating reversible obfuscated identifier hashes
to a given base and length. The project is based on the GO library, [PseudoCrypt][pc]
by [Kevin Burns][kb]. The library is extendible to use custom alphabets and other
bases.

The library uses golden primes and the [Baillie-PSW][bp] primality test for hashing
to `n` length. From testing, I have gotten `base62` up to `171` in length.

```
Maximum number is Base^Length - 1.
-> 62^171 - 1 or 315485137315301582773830923281251564555089304044116975095028710
008180170985809814948409129256031320171601473029340987051144213
425607224233134700199050224309707192084206558324823774511143549
765069844412467187455459156942237963528166277256376429656681225
8180788198965409784329587392583208081351811265973977087
```


Install
=======

```
pip install basehash
```

Testing
=======

```
nosetests tests/
```

Encode
------
```python
from basehash import base62

encoded = base62.encode(2013)
decoded = base62.decode('WT')

print encoded, decoded
```
```
WT 2013
```

Hash
----
```python
from basehash import base62

hashed = base62.hash(2013, 8)
unhashed = base62.unhash('6LhOma5b')

print hashed, unhashed
```
```
6LhOma5b 2013
```

Generating your own primes
--------------------------
The default primes are generated using the golden ratio, `1.618033988749894848`
but this can be changed with `basehash.base.GENERATOR`

```python
# Generate primes, default golden ratio.
GENERATOR = 1.618033988749894848 # Change to whatever you'd like
```

Maximum number while hashing
----------------------------
There is a maximum number while hashing with any given base. To find out what
this number is, we use the `Base^Length - 1` inside the `base_maximum(length)`
method

```python
from basehash import base36

print base36.maximum(12)
```
```
4738381338321616895
```

So with the max number for `base36` at length `12` as `4738381338321616895` we
get the following:

```python
from basehash import base36

hash = base36.hash(4738381338321616895, 12)
# 'DR10828P4CZP'

hash = base36.hash(4738381338321616896, 12)
# ValueError: Number is too large for given length. Maximum is 36^12 - 1.
```

Extending
---------

```python
from basehash.base import *

ALPHA = tuple('24680ACEGIKMOQSUWYbdfhjlnprtvxz')

# Length 'base' is 31 -> len(ALPHA)

def encode(num):
return base_encode(num, ALPHA)

def decode(key):
return base_decode(key, ALPHA)

def hash(num, length=HASH_LENGTH):
return base_hash(num, length, ALPHA)

def unhash(key):
return base_unhash(key, ALPHA)

def maximum(length=HASH_LENGTH):
return base_maximum(len(ALPHA), length)
```

[pc]: https://github.com/KevBurnsJr/pseudocrypt
[kb]: https://github.com/KevBurnsJr
[bp]: http://en.wikipedia.org/wiki/Baillie-PSW_primality_test


1.0.7 (2013-07-06)
++++++++++++++++++

- There was an issue with hashes sometimes being returned one to two charcters
shorter than `length`, causing `base.base_unhash` to not function properly. To
fix this, the hashes are right-padded with `0`.

- Since `0` raises an error inside `primes.invmul`, `base.base_unhash` is unable
to unhash it. To allow the start of your number sequence to be `0` instead of
`1`, if needed, hashing `base.base_hash(0, length=6)` will return
`''.rjust(length, alphabet[0])`.

1.0.6 (2013-06-29)
++++++++++++++++++

- Fixed issues with setup.py. First time using a setup.py within a package,
first time publishing the library outside of GitHub.

1.0.5 (2013-06-28)
++++++++++++++++++

- Added nose unittests.

1.0.4 (2013-06-28)
++++++++++++++++++

- Added setup.py, LICENSE, HISTORY.rst, and .travis.yaml.

1.0.3 (2013-06-27)
++++++++++++++++++

- Added a simple test for `prime < 31` to reduce calculation time.

- Fixed issue of `strong_pseudoprime(n, 3)` giving false results.

1.0.2 (2013-06-27)
++++++++++++++++++

- Changed primality test from Miller-Rabin to Baillie-PSW. This algorithm is
significantly faster.

- Changed determination to use `sqrt(n)` or `isqrt(n)` to an improved version of
`isqrt(n)`.

- BaseHash is now PEP compliant.

1.0.1 (2013-06-25)
++++++++++++++++++

- Changed primality test from Fermat to Miller-Rabin. Improved accuracy on false
results when it comes to pseudoprimes.

1.0.0 (2013-06-24)
++++++++++++++++++

- Released code to GitHub repository python-basehash
https://github.com/bnlucas/python-basehash

0.0.1 (2013-06-23)
++++++++++++++++++

- Initialization

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

BaseHash-1.0.7.zip (19.3 kB view hashes)

Uploaded Source

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