Skip to main content

Python implementation of hashids (http://www.hashids.org).Compatible with python 2.5--3.

Project description

Website: http://www.hashids.org/

Generate short hashes from unsigned integers (like YouTube and Bitly).

  • obfuscate database IDs

  • use them as forgotten password hashes

  • invitation codes

  • store shard numbers

Hashids was designed for use in URL shortening, tracking stuff, validating accounts or making pages private. Instead of showing items as 1, 2, or 3, you could show them as b9iLXiAa, EATedTBy, and Aaco9cy5. Hashes depend on your salt value.

Usage

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

pip install hashids

Import the constructor from the hashids module:

from hashids import Hashids
hashids = Hashids()

Basic Usage

Encrypt a single integer:

hashid = hashids.encrypt(123) # 'AjL'

Decrypt a hash:

ints = hashids.decrypt('b9a') # (456,)

To encrypt several integers, pass them all at once:

hashid = hashids.encrypt(123, 456, 789) # 'qa9t96h7G'

Decryption is done the same way:

ints = hashids.decrypt('yn8t46hen') # (517, 729, 185)

Using A Custom Salt

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

hashids = Hashids(salt='this is my salt 1')
hashid = hashids.encrypt(123) # 'rnR'

The generated hash changes whenever the salt is changed:

hashids = Hashids(salt='this is my salt 2')
hashid = hashids.encrypt(123) # 'XBn'

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.encrypt(1) # 'Ee7uE4iyEiEG7ued'

Using A Custom Alphabet

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

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

hashids = Hashids(alphabet='abcdefghijklmnopqrstuvwxyz')
hashid = hashids.encrypt(123456789) # 'dpovunuo'

A custom alphabet must contain at least 4 letters, but should contain at least 16 characters.

#%@&

This code was written with the intent of placing generated hashes in visible places – like the URL.

Therefore, the algorithm tries to avoid generating most common English curse words by never placing the following letters next to each other: c, C, s, S, f, F, h, H, u, U, i, I, t, T.

Collisions

There are no collisions. Your generated hashes should be unique.

Decryptable Hash ¿qué?

A true cryptographic hash cannot be decrypted. However, to keep things simple the word hash is used loosely to refer to the random set of characters that are generated. Like a YouTube hash.

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

hashids-0.8.4.tar.gz (5.9 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