All Projects → vinkla → Hashids

vinkla / Hashids

Licence: mit
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.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to Hashids

Hashids.js
A small JavaScript library to generate YouTube-like ids from numbers.
Stars: ✭ 3,525 (-23.3%)
Mutual labels:  hash, ids, hashids, encoding, database-ids
hashids.pm
Hashids, ported for Perl
Stars: ✭ 15 (-99.67%)
Mutual labels:  encoding, hashids, hash, ids, database-ids
harsh
Hashids implementation in Rust
Stars: ✭ 48 (-98.96%)
Mutual labels:  encoding, hashids, hash, ids, database-ids
Codetective
a tool to determine the crypto/encoding algorithm used according to traces from its representation
Stars: ✭ 121 (-97.37%)
Mutual labels:  hash, encoding
Optimus
🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.
Stars: ✭ 1,084 (-76.41%)
Mutual labels:  ids, hashids
Hashids.net
A small .NET package to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Stars: ✭ 470 (-89.77%)
Mutual labels:  hashids, encoding
Hashlib4pascal
Hashing for Modern Object Pascal
Stars: ✭ 132 (-97.13%)
Mutual labels:  hash, encoding
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (-99.15%)
Mutual labels:  hashids, database-ids
bytes-java
Bytes is a utility library that makes it easy to create, parse, transform, validate and convert byte arrays in Java. It supports endianness as well as immutability and mutability, so the caller may decide to favor performance.
Stars: ✭ 120 (-97.39%)
Mutual labels:  encoding, hash
laravel-hashid
HashId Implementation on Laravel Eloquent ORM
Stars: ✭ 23 (-99.5%)
Mutual labels:  hashids, hash
idy
👓 An ID obfuscator for ActiveRecord
Stars: ✭ 15 (-99.67%)
Mutual labels:  hashids, hash
Chronicle
Public append-only ledger microservice built with Slim Framework
Stars: ✭ 429 (-90.67%)
Mutual labels:  hash
Suricata Rules
Suricata IDS rules 用来检测红队渗透/恶意行为等,支持检测CobaltStrike/MSF/Empire/DNS隧道/Weevely/菜刀/冰蝎/挖矿/反弹shell/ICMP隧道等
Stars: ✭ 397 (-91.36%)
Mutual labels:  ids
Goimagehash
Go Perceptual image hashing package
Stars: ✭ 391 (-91.49%)
Mutual labels:  hash
Minih264
Minimalistic H264/SVC encoder single header library
Stars: ✭ 390 (-91.51%)
Mutual labels:  encoding
Bitcracker
BitCracker is the first open source password cracking tool for memory units encrypted with BitLocker
Stars: ✭ 463 (-89.93%)
Mutual labels:  hash
Binary Parser
Blazing-fast declarative parser builder for binary data
Stars: ✭ 422 (-90.82%)
Mutual labels:  encoding
Awesome Cybersecurity Datasets
A curated list of amazingly awesome Cybersecurity datasets
Stars: ✭ 380 (-91.73%)
Mutual labels:  ids
Psl
PHP Standard Library - a modern, consistent, centralized, well-typed set of APIs for PHP programmers.
Stars: ✭ 329 (-92.84%)
Mutual labels:  encoding
Libmorton
C++ header-only library with methods to efficiently encode/decode Morton codes in/from 2D/3D coordinates
Stars: ✭ 373 (-91.88%)
Mutual labels:  encoding

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 requires either the BC Math 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].