Welcome to BaseHash’s documentation!¶
BaseHash is available on PyPi, to install simply do pip install basehash
.
The source is availabile at the GitHub repository python-basehash.
Contents:
The heart of BaseHash¶
BaseHash Constants¶
The two constants of BaseHash are HASH_LENGTH
and GENERATOR
.
HASH_LENGTH
, default set to 6
, is used as a default hashing length,
which can be overridden in baseN.hash()
.
GENERATOR
uses the Golden Ratio, 1.618033988749894848
, to determine
the next highest prime, which is based on base ^ length - 1
. GENERATOR
can either be overridden globally or can be overridden within base_hash
or
base_unhash
.
prime¶
-
prime
(base, n, gen) Returns next highest prime. using base^n * gen.
base_decode¶
-
base_decode
(key, alphabet)¶ Decodes string
key
from stringalphabet
(orbase
). Returnsint
base_hash¶
-
base_hash
(num, length, alphabet[, gen=GENERATOR])¶ Hashes int
num
to stringalphabet
(orbase
), intlength
digits long using the built inbase.GENERATOR
, which can be overridden. Returnsstring
Built-in BaseN
¶
BaseHash comes with a few built-in bases, Base36
, Base52
, Base56
,
Base58
, Base62
, and Base94
.
BaseN.BASEN¶
BASE36
= 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
BASE52
= 0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz
BASE56
= 23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz
BASE58
= 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
BASE62
= 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
BASE94
= !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
encode¶
-
baseN.
encode
(num)¶ Encodes int
num
tobaseN
. Returnsbase_encode(num, BASEN)
. Returnsstring
decode¶
-
baseN.
decode
(key)¶ Decodes string key from
baseN
. Returnsbase_decode(key, BASEN)
. Returnsint
hash¶
-
baseN.
hash
(num[, length=HASH_LENGTH])¶ Hashes int
num
tobaseN
at intlength
characters. Returnsbase_hash(num, length, BASEN)
. Returnsstring
Extending to BaseX
¶
Much work was put into generating prime numbers on the fly, allowing BaseHash to
be extended to BaseX
with ease. To extend the library, you just need to
import basehash.base
and call a few mthods.
from basehash.base import *
# ALPHA must be a tuple
ALPHA = tuple('24680ACEGIKMOQSUWYbdfhjlnprtvxz')
# hash `num` to `ALPHA` at `length` characters
def hash(num, length=HASH_LENGTH):
return base_hash(num, length, ALPHA)
# unhash `key` from `ALPHA`
def unhash(key):
return base_unhash(key, ALPHA)
## optional methods:
# encode `num` to `ALPHA`
def encode(num):
return base_encode(num, ALPHA)
# decode `key` from `ALPHA`
def decode(key):
return base_decode(key, ALPHA)
# return maximum value for `hash` at `length`
def maximum(length=HASH_LENGTH):
return base_maximum(len(ALPHA), length)