Skip to main content

Implements the hashids algorithm in python with support of string.

Project description

A string support addition to the hashids algorithm (A python port of the JavaScript hashids implementation). Website: http://www.hashids.org/

Compatibility

hashids is tested with python 2.7 and 3.5–3.8. PyPy and PyPy 3 work as well.

Compatibility with the JavaScript implementation

hashids/JavaScript

hashids/Python

v0.1.x

v0.8.x

v0.3.x+

v1.0.2+

The JavaScript implementation produces different hashes in versions 0.1.x and 0.3.x. For compatibility with the older 0.1.x version install hashids 0.8.4 from pip, otherwise the newest hashids.

Installation

Install the module from PyPI, e. g. with pip:

pip install hashidstr

Usage

Import the constructor from the hashids module:

from hashidstr import Hashids
hashids = Hashids()

Basic Usage

Encode a single integer:

hashid = hashids.encode(123) # 'Mj3'

Decode a hash:

ints = hashids.decode('xoz') # (456,)

To encode several integers, pass them all at once:

hashid = hashids.encode([123, 456, 789]) # 'El3fkRIo3'

Decoding is done the same way:

ints = hashids.decode('1B8UvJfXm') # (517, 729, 185)

Encode String:

hashid = hashids.encode_string("test string") # 'k5nInZiVpUpBfos9KS6kcgqs0WCX8UEw'

Decode String:

string = hashids.decode_string('k5nInZiVpUpBfos9KS6kcgqs0WCX8UEw') # 'test string'

Using A Custom Salt

Hashids supports salting hashes by accepting a salt value. If you don’t want others to decode your hashes, provide a unique string to the constructor.

hashids = Hashids(salt='this is my salt 1')
hashid = hashids.encode(123) # 'nVB'

The generated hash changes whenever the salt is changed:

hashids = Hashids(salt='this is my salt 2')
hashid = hashids.encode(123) # 'ojK'

A salt string between 6 and 32 characters provides decent randomization.

Controlling Hash Length

By default, hashes are going to be the shortest possible. One reason you might want to increase the hash length is to obfuscate how large the integer behind the hash is.

This is done by passing the minimum hash length to the constructor. Hashes are padded with extra characters to make them seem longer.

hashids = Hashids(min_length=16)
hashid = hashids.encode(1) # '4q2VolejRejNmGQB'

Using A Custom Alphabet

It’s possible to set a custom alphabet for your hashes. The default alphabet is 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'.

To have only lowercase letters in your hashes, pass in the following custom alphabet:

hashids = Hashids(alphabet='abcdefghijklmnopqrstuvwxyz')
hashid = hashids.encode(123456789) # 'kekmyzyk'

A custom alphabet must contain at least 16 characters.

Randomness

The primary purpose of hashids is to obfuscate ids. It’s not meant or tested to be used for security purposes or compression. Having said that, this algorithm does try to make these hashes unguessable and unpredictable:

Repeating numbers

There are no repeating patterns that might show that there are 4 identical numbers in the hash:

hashids = Hashids("this is my salt")
hashids.encode(5, 5, 5, 5) # '1Wc8cwcE'

The same is valid for incremented numbers:

hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # 'kRHnurhptKcjIDTWC3sx'

hashids.encode(1) # 'NV'
hashids.encode(2) # '6m'
hashids.encode(3) # 'yD'
hashids.encode(4) # '2l'
hashids.encode(5) # 'rD'

TODO

  • Add back support to the integer encode function

License

MIT license, see the LICENSE file. You can use hashids in open source projects and commercial products.

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

hashidstr-1.3.3.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hashidstr-1.3.3-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file hashidstr-1.3.3.tar.gz.

File metadata

  • Download URL: hashidstr-1.3.3.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for hashidstr-1.3.3.tar.gz
Algorithm Hash digest
SHA256 427c6fb05fc9eee17756f70758d95945397a5e5de2da48ae1495b67e98428770
MD5 823beda0a6f16c7a479b0c480a04f990
BLAKE2b-256 c62d7de569a42d6baafc8ed505a28bb4f141f575e5cc60b8efc501330979051a

See more details on using hashes here.

File details

Details for the file hashidstr-1.3.3-py2.py3-none-any.whl.

File metadata

  • Download URL: hashidstr-1.3.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for hashidstr-1.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cccc52c1b00fd5a900616a0dc6a3e993a6c7f6ff54a7ebdea10ab9fce7018a43
MD5 d27d9cea1b713f0eeb0d7d8d7ac6b714
BLAKE2b-256 2cca74cc4a4703d7ba592ff1defc4929de5f4179f52c4d30d0a71cd15684ef7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page