Skip to main content

Incremental cryptography and flexible hash

Project description

test codecov

garoupa

Incremental cryptography and flexible hash

Malabar grouper melb aquarium

Latest version

Installation

as a standalone lib.

# Set up a virtualenv. 
python3.7 -m venv venv
source venv/bin/activate

# Install from PyPI...
pip install --upgrade pip
pip install -U garoupa

# ...or, install from updated source code.
pip install git+https://github.com/davips/garoupa

as an editable lib inside your project.

cd your-project
source venv/bin/activate
git clone https://github.com/davips/garoupa ../garoupa
pip install --upgrade pip
pip install -e ../garoupa

Examples

Creating flexible hashes

from garoupa import Hash

# Binary strings are hashed by MD5.
a = Hash(b"Some text.")
print(f"Hex:\n{a.hex}")
"""
Hex:
5f2b51ca2fdc5baa31ec02e002f69aec
"""
# A shorter base-62 identifier is also provided as default.
print(a.id, "=", a)
"""
2ta4DsTtzJxNXItOSQfcfE = 2ta4DsTtzJxNXItOSQfcfE
"""
# Integers are not hashed, they are directly mapped to the hash space.
print(a.n)
"""
126501587258562921197401139372367452908
"""
print(Hash(126501587258562921197401139372367452908), "=", a)
"""
2ta4DsTtzJxNXItOSQfcfE = 2ta4DsTtzJxNXItOSQfcfE
"""
b = Hash(340282366920938463463374607431768211455)  # Largest posible number.
print(b)
"""
7n42DGM5Tflk9n8mt7Fhc7
"""

Operations between flexible hashes

from garoupa import Hash

# Hashes can be multiplied.
a = Hash(b"Some text.")
b = Hash(b"Other text.")
c = a * b
print(f"{a} * {b} = {c}")
"""
2ta4DsTtzJxNXItOSQfcfE * 1s2qEnAwwi16V2hCUYV8dY = 3EYMMabJR2Aj8rUtPiZ1gO
"""
# Multiplication can be reverted by the inverse hash. Zero is the identity hash.
print(f"{b} * {b.inv} = {b * b.inv} = 0")
"""
1s2qEnAwwi16V2hCUYV8dY * 1he9mgDNZUsKtjzHTFTWsW = 0000000000000000000000 = 0
"""
print(f"{c} * {b.inv} = {c * b.inv} = {a} = a")
"""
3EYMMabJR2Aj8rUtPiZ1gO * 1he9mgDNZUsKtjzHTFTWsW = 2ta4DsTtzJxNXItOSQfcfE = 2ta4DsTtzJxNXItOSQfcfE = a
"""
print(f"{a.inv} * {c} = {a.inv * c} = {b} = b")
"""
2PMEzPHCsQn8cSxy31ohvo * 3EYMMabJR2Aj8rUtPiZ1gO = 1s2qEnAwwi16V2hCUYV8dY = 1s2qEnAwwi16V2hCUYV8dY = b
"""
# Division is shorthand for reversion.
print(f"{c} / {b} = {c / b} = a")
"""
3EYMMabJR2Aj8rUtPiZ1gO / 1s2qEnAwwi16V2hCUYV8dY = 2ta4DsTtzJxNXItOSQfcfE = a
"""
# Hash multiplication is not commutative.
print(f"{a * b} != {b * a}")
"""
3EYMMabJR2Aj8rUtPiZ1gO != 3SemNwugPbPPwm6wXaYFqi
"""
# Hash multiplication is associative.
print(f"{a * (b * c)} = {(a * b) * c}")
"""
021EieFLdrV69bPx1ddPJA = 021EieFLdrV69bPx1ddPJA
"""

Timing tradeoff startup/repetition

from timeit import timeit

from garoupa import Hash


def f():
    return Hash(12431434) * Hash(895784)


def f_compiled():
    return Hash(12431434, compiled=True) * Hash(895784, compiled=True)

t = timeit(f, number=1)
print("Normal warm up time:", round(t, 2), "s")
"""
Normal warm up time: 0.0 s
"""
t = timeit(f, number=100000)
print("Normal time:", round(t * 10, 2), "us")
"""
Normal time: 52.25 us
"""
t = timeit(f_compiled, number=1)
print("Compiled warm up time:", round(t, 2), "s")
"""
Compiled warm up time: 2.28 s
"""
t = timeit(f_compiled, number=100000)
print("Compiled time:", round(t * 10, 2), "us")
"""
Compiled time: 7.59 us
"""

Features / TODO

  • Features / TODO

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

garoupa-0.2101.12.tar.gz (29.8 kB view hashes)

Uploaded Source

Built Distribution

garoupa-0.2101.12-py3-none-any.whl (48.8 kB view hashes)

Uploaded Python 3

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