Convert Figma logo to code with AI

vinkla logohashids

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.

5,276
418
5,276
0

Top Related Projects

1,267

🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.

5,276

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.

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

Quick Overview

Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It's designed to create YouTube-like ids in your database and can be used as a primary key or a obfuscated identifier. The library is available in multiple programming languages, including PHP, JavaScript, Python, and more.

Pros

  • Generates short, URL-friendly ids from numbers
  • Supports custom salt and minimum id length for added security
  • Can encode multiple numbers into a single id
  • Available in many programming languages

Cons

  • Not suitable for sensitive data encryption
  • Generated ids can be decoded back to original numbers
  • Limited customization options for id format
  • May produce longer ids for very large numbers

Code Examples

  1. Basic usage:
use Hashids\Hashids;

$hashids = new Hashids();

$id = $hashids->encode(1, 2, 3); // 'o2fXhV'
$numbers = $hashids->decode($id); // [1, 2, 3]
  1. Custom salt and minimum length:
$hashids = new Hashids('my salt', 8);

$id = $hashids->encode(1); // 'KPv7QL2r'
$numbers = $hashids->decode($id); // [1]
  1. Encoding and decoding a list of numbers:
$hashids = new Hashids();

$numbers = [1, 2, 3, 4, 5];
$id = $hashids->encode($numbers); // 'zoHWuNhktp'
$decodedNumbers = $hashids->decode($id); // [1, 2, 3, 4, 5]

Getting Started

To use Hashids in your PHP project, follow these steps:

  1. Install the library using Composer:

    composer require hashids/hashids
    
  2. In your PHP file, use the Hashids class:

    use Hashids\Hashids;
    
    $hashids = new Hashids();
    
    $id = $hashids->encode(1, 2, 3);
    echo $id; // Outputs: 'o2fXhV'
    
    $numbers = $hashids->decode($id);
    print_r($numbers); // Outputs: Array ( [0] => 1 [1] => 2 [2] => 3 )
    

That's it! You can now start using Hashids to generate and decode ids in your project.

Competitor Comparisons

1,267

🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.

Pros of Optimus

  • Offers more flexibility in encoding/decoding methods (e.g., base62, base64, hex)
  • Provides additional features like connection encoding and custom alphabets
  • Supports Laravel integration out of the box

Cons of Optimus

  • Less widespread adoption compared to Hashids
  • Documentation is not as extensive or well-organized
  • May have a steeper learning curve for beginners

Code Comparison

Hashids:

$hashids = new Hashids\Hashids('salt');
$encoded = $hashids->encode(1, 2, 3);
$decoded = $hashids->decode($encoded);

Optimus:

$optimus = new Jenssegers\Optimus\Optimus($prime, $inverse, $random);
$encoded = $optimus->encode(1234);
$decoded = $optimus->decode($encoded);

Both libraries offer similar basic functionality for encoding and decoding integers. However, Optimus uses a different approach based on Knuth's multiplicative hashing method, while Hashids uses a custom algorithm to generate short, unique, non-sequential ids.

Optimus provides more advanced features and encoding options, making it suitable for complex use cases. Hashids, on the other hand, focuses on simplicity and ease of use, making it a popular choice for basic id obfuscation needs.

5,276

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 a wider range of programming languages
  • Has a larger community and more contributors

Cons of hashids

  • Slightly more complex API
  • May have a steeper learning curve for beginners
  • Potentially higher overhead due to additional features

Code Comparison

hashids:

$hashids = new Hashids('this is my salt');
$id = $hashids->encode(1, 2, 3);
$numbers = $hashids->decode($id);

hashids>:

$hashids = new Hashids('this is my salt');
$id = $hashids->encode(1, 2, 3);
$numbers = $hashids->decode($id);

The code usage for both libraries is identical in this basic example. However, hashids offers more advanced features and configuration options that may result in slightly different implementation in more complex scenarios.

Both libraries serve the same primary purpose of generating short, unique, non-sequential identifiers from numbers. The choice between them largely depends on specific project requirements, desired language support, and personal preference for API design and feature set.

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

Pros of hashids-python

  • Written in Python, making it more suitable for Python projects
  • Supports Python 2.7 and 3.3+, offering broader compatibility
  • Includes type hints for improved code readability and IDE support

Cons of hashids-python

  • Less actively maintained, with fewer recent updates
  • Smaller community and fewer contributors compared to hashids

Code Comparison

hashids-python:

from hashids import Hashids
hashids = Hashids(salt="my salt")
encoded = hashids.encode(1, 2, 3)
decoded = hashids.decode(encoded)

hashids:

use Hashids\Hashids;
$hashids = new Hashids('my salt');
$encoded = $hashids->encode([1, 2, 3]);
$decoded = $hashids->decode($encoded);

Both libraries offer similar functionality for encoding and decoding IDs. The main difference lies in the language and syntax used. hashids-python is ideal for Python projects, while hashids is better suited for PHP applications. The choice between the two largely depends on the programming language of your project and your specific requirements.

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

Build Status Monthly Downloads Latest Version

Hashids is a small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database numeric ids to users: https://hashids.org/php

Getting started

Require this package, with Composer, in the root directory of your project.

composer require hashids/hashids

Then you can import the class into your application:

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1);

Note Hashids require either bcmath or gmp extension in order to work.

Quick Example

use Hashids\Hashids;

$hashids = new Hashids();

$id = $hashids->encode(1, 2, 3); // o2fXhV
$numbers = $hashids->decode($id); // [1, 2, 3]

More Options

A few more ways to pass input ids to the encode() function:

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1, 2, 3); // o2fXhV
$hashids->encode([1, 2, 3]); // o2fXhV
$hashids->encode('1', '2', '3'); // o2fXhV
$hashids->encode(['1', '2', '3']); // o2fXhV

Making your output ids unique

Pass a project name to make your output ids unique:

use Hashids\Hashids;

$hashids = new Hashids('My Project');
$hashids->encode(1, 2, 3); // Z4UrtW

$hashids = new Hashids('My Other Project');
$hashids->encode(1, 2, 3); // gPUasb

Use padding to make your output ids longer

Note that output ids are only padded to fit at least a certain length. It doesn't mean that they will be exactly that length.

use Hashids\Hashids;

$hashids = new Hashids(); // no padding
$hashids->encode(1); // jR

$hashids = new Hashids('', 10); // pad to length 10
$hashids->encode(1); // VolejRejNm

Using a custom alphabet

use Hashids\Hashids;

$hashids = new Hashids('', 0, 'abcdefghijklmnopqrstuvwxyz'); // all lowercase
$hashids->encode(1, 2, 3); // mdfphx

Encode hex instead of numbers

Useful if you want to encode Mongo's ObjectIds. Note that there is no limit on how large of a hex number you can pass (it does not have to be Mongo's ObjectId).

use Hashids\Hashids;

$hashids = new Hashids();

$id = $hashids->encodeHex('507f1f77bcf86cd799439011'); // y42LW46J9luq3Xq9XMly
$hex = $hashids->decodeHex($id); // 507f1f77bcf86cd799439011

Pitfalls

  1. When decoding, output is always an array of numbers (even if you encoded only one number):

    use Hashids\Hashids;
    
    $hashids = new Hashids();
    
    $id = $hashids->encode(1);
    
    $hashids->decode($id); // [1]
    
  2. Encoding negative numbers is not supported.

  3. If you pass bogus input to encode(), an empty string will be returned:

    use Hashids\Hashids;
    
    $hashids = new Hashids();
    
    $id = $hashids->encode('123a');
    
    $id === ''; // true
    
  4. Do not use this library as a security measure. Do not encode sensitive data with it. Hashids is not an encryption library.

Randomness

The primary purpose of Hashids is to obfuscate numeric ids. It's not meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable:

There is no pattern shown when encoding multiple identical numbers (3 shown in the following example):

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(5, 5, 5); // A6t1tQ

The same is true when encoding a series of numbers vs. encoding them separately:

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // wpfLh9iwsqt0uyCEFjHM

$hashids->encode(1); // jR
$hashids->encode(2); // k5
$hashids->encode(3); // l5
$hashids->encode(4); // mO
$hashids->encode(5); // nR

Curse words! #$%@

This code was written with the intent of placing the output ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other:

c, f, h, i, s, t, u

NPM DownloadsLast 30 Days