GithubHelp home page GithubHelp logo

kobatei / python-basehash Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bnlucas/python-basehash

0.0 0.0 0.0 166 KB

Extendible base hashing in Python.

Home Page: http://bnlucas.github.io/python-basehash

License: Other

Python 100.00%

python-basehash's Introduction

BaseHash

Build Status Bitdeli Badge

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 by Kevin Burns. The library is extendible to use custom alphabets and other bases.

The library uses golden primes and the Baillie-PSW primality test or the gmpy2.is_prime (if available) for hashing to maximum length (base ** length - 1).

v3.3.0

A massive overhaul was done with the primality algorithms. Including support for gmpy2 if it available on the system for that much more of an increase.

All methods being used to check primality in primes.py have been optimized and benchmarked to try to get the best possible preformance when gmpy2.is_prime and gmpy2.next_prime are not available.

v3.0.0 vs v2.2.0 without gmpy2

--------------------------------------------------------------------------------
 basehash 3.0.0 vs basehash 2.2.1 speed comparison. (without gmpy2)
 testing against random 128-bit integer with BASE62 and length of 30.

 comparing best 100 of 1000 loops.
--------------------------------------------------------------------------------
 bh300                                                    @        0.011989977s 
 bh220                                                    @        0.019100001s 
--------------------------------------------------------------------------------

v3.0.0 vs v2.2.0 with gmpy2

--------------------------------------------------------------------------------
 basehash 3.0.0 vs basehash 2.2.1 speed comparison. (with gmpy2)
 testing against random 128-bit integer with BASE62 and length of 30.

 comparing best 100 of 1000 loops.
--------------------------------------------------------------------------------
 bh300                                                    @        0.002969882s 
 bh220                                                    @        0.018960006s 
--------------------------------------------------------------------------------

Install

pip install basehash

Testing

nosetests tests/

Encode

import basehash

base62 = basehash.base62(8)

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

print encoded, decoded
WT 2013

Hash

import basehash

base62 = basehash.base62(8)

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

print hashed, unhashed
6LhOma5b 2013

Generating your own primes

The GENERATOR variable uses the golden ratio, 1.618033988749894848, to get the next highest prime of base ** number * generator. This can be overridden within the base classes.

import basehash

base62 = basehash.base62(generator=1.75)

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.

import basehash

base36 = basehash.base36(10)

print base36.maximum
4738381338321616895

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

import basehash

base36 = basehash.base36(12)

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

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

Extending

Extending is made easy with some time spent determining the next highest prime dynamically, the fastest possible that I have been able to make it so far.

import basehash

custom = basehash.base('24680ACEGIKMOQSUWYbdfhjlnprtvxz', 8)

print custom.encode(2013)       # 66x
print custom.decode('66x')      # 2013
print custom.hash(2013)      # 8AQAQdYd
print custom.unhash('8AQAQdYd') # 2013
print custom.maximum         # 787662783788549760

python-basehash's People

Contributors

fretscha avatar bnlucas avatar calve avatar kobatei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.