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.3.tar.gz (5.9 kB view details)

Uploaded Source

File details

Details for the file hashids-0.8.3.tar.gz.

File metadata

  • Download URL: hashids-0.8.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hashids-0.8.3.tar.gz
Algorithm Hash digest
SHA256 93aa48138f73ed3f0bf083215e08510e73d4be03021ed9822ab1c822d75da1a2
MD5 1f1da1ca12b37aa6c3e3119b504d7e8a
BLAKE2b-256 c9fcaa475ade8f898fd4e33f00af9799d6991f0db686c0feddd6db96ffa4a443

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