Convert Figma logo to code with AI

davidaurelio logohashids-python

Implementation of hashids (http://hashids.org) in Python. Compatible with Python 2 and Python 3

1,408
106
1,408
8

Top Related Projects

5,255

A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.

A small JavaScript library to generate YouTube-like ids from numbers.

Quick Overview

Hashids-python is a Python implementation of the Hashids algorithm, which generates short, unique, non-sequential identifiers from numbers. It's useful for creating obfuscated IDs for public URLs, hiding database IDs, or generating short, unique strings from numeric data.

Pros

  • Generates short, URL-safe strings from numbers
  • Supports custom salt, minimum length, and alphabet
  • Can encode multiple numbers into a single hash
  • Bidirectional: can decode hashes back to original numbers

Cons

  • Not suitable for security-critical applications (not cryptographically secure)
  • Limited to encoding integers only
  • Performance may degrade with very large numbers or long minimum lengths
  • Requires careful configuration to avoid collisions in certain use cases

Code Examples

Encoding a single number:

from hashids import Hashids

hashids = Hashids()
hash = hashids.encode(12345)
print(hash)  # Output: "NkK9"

Decoding a hash:

from hashids import Hashids

hashids = Hashids()
numbers = hashids.decode("NkK9")
print(numbers)  # Output: (12345,)

Encoding multiple numbers with custom salt and minimum length:

from hashids import Hashids

hashids = Hashids(salt="my custom salt", min_length=10)
hash = hashids.encode(1, 2, 3)
print(hash)  # Output: "3kK9MmvVPwx"

Getting Started

To use hashids-python in your project, follow these steps:

  1. Install the package:

    pip install hashids
    
  2. Import and use in your Python code:

    from hashids import Hashids
    
    # Create a Hashids instance (optionally with salt and min_length)
    hashids = Hashids(salt="my salt", min_length=8)
    
    # Encode numbers
    encoded = hashids.encode(1234, 5678)
    
    # Decode the hash
    decoded = hashids.decode(encoded)
    
    print(f"Encoded: {encoded}")
    print(f"Decoded: {decoded}")
    

This will get you started with basic encoding and decoding using hashids-python. Refer to the project's documentation for more advanced usage and configuration options.

Competitor Comparisons

5,255

A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.

Pros of Hashids

  • More actively maintained with recent updates
  • Supports multiple programming languages (PHP, JavaScript, etc.)
  • Comprehensive documentation and examples

Cons of Hashids

  • Larger codebase, potentially more complex
  • May have more dependencies
  • Slightly different API structure

Code Comparison

hashids-python:

from hashids import Hashids
hashids = Hashids()
encoded = hashids.encode(12345)
decoded = hashids.decode(encoded)

Hashids:

use Hashids\Hashids;
$hashids = new Hashids();
$encoded = $hashids->encode(12345);
$decoded = $hashids->decode($encoded);

Both libraries provide similar functionality for encoding and decoding IDs. The main differences lie in the language implementation and specific method names. hashids-python is Python-specific, while Hashids supports multiple languages, with the example shown in PHP.

The core functionality remains consistent across both libraries, allowing users to generate short, unique, non-sequential ids from numbers. Both are suitable for tasks like obfuscating database IDs or creating short URLs. The choice between them may depend on the specific programming language requirements and preference for a single-language or multi-language solution.

A small JavaScript library to generate YouTube-like ids from numbers.

Pros of hashids.js

  • Written in TypeScript, providing better type safety and developer experience
  • More active development and maintenance, with recent updates and contributions
  • Supports both Node.js and browser environments

Cons of hashids.js

  • Larger package size due to TypeScript compilation and additional features
  • May have slightly higher performance overhead compared to the Python implementation

Code Comparison

hashids.js:

import Hashids from 'hashids';
const hashids = new Hashids();
const id = hashids.encode(1, 2, 3);
const numbers = hashids.decode(id);

hashids-python:

from hashids import Hashids
hashids = Hashids()
id = hashids.encode(1, 2, 3)
numbers = hashids.decode(id)

Both implementations offer similar functionality and usage patterns. The main differences lie in the language-specific syntax and ecosystem. hashids.js benefits from TypeScript's type system and broader JavaScript ecosystem support, while hashids-python provides a simpler, more lightweight implementation for Python projects.

When choosing between the two, consider your project's language requirements, development environment, and specific needs for type safety and ecosystem integration.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

========================== hashids for Python 2.7 & 3

A python port of the JavaScript hashids implementation. It generates YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user. 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.

.. image:: https://travis-ci.org/davidaurelio/hashids-python.svg?branch=master :target: https://travis-ci.org/davidaurelio/hashids-python

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:

.. code:: bash

pip install hashids pip install hashids==0.8.4 # for compatibility with hashids.js 0.1.x

Run the tests

The tests are written with pytest <http://pytest.org/latest/>_. The pytest module has to be installed.

.. code:: bash

python -m pytest

Usage

Import the constructor from the hashids module:

.. code:: python

from hashids import Hashids hashids = Hashids()

Basic Usage

Encode a single integer:

.. code:: python

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

Decode a hash:

.. code:: python

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

To encode several integers, pass them all at once:

.. code:: python

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

Decoding is done the same way:

.. code:: python

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

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.

.. code:: python

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

The generated hash changes whenever the salt is changed:

.. code:: python

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.

.. code:: python

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:

.. code:: python

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:

.. code:: python

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

The same is valid for incremented numbers:

.. code:: python

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'

Curses! #$%@

This code was written with the intent of placing generated hashes in visible places – like the URL. Which makes it unfortunate if generated hashes accidentally formed a bad word.

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.

License

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