Welcome to the comprehensive tutorial on Node.js Hashids, an encoding library that can help you generate short, unique, and non-sequential ids from numbers. Whether you’re a seasoned developer or someone who’s just starting out, this guide aims to walk you through the basics and dive into some practical examples.
Update: hashids is rebranded as Sqids. Sqids (pronounced “squids”) is an open-source library that lets you generate YouTube-looking IDs from numbers. These IDs are short, can be generated from a custom alphabet and are guaranteed to be collision-free. There is no change in the function signature, but the package name is changed to sqids.
Table of Contents
- Introduction to Node.js Hashids
- Installation
- Basic Syntax
- Practical Examples
- Further Resources
- Conclusive Summary
Introduction to Node.js Hashids (sqids)
Hashids is a library designed to create short hashes from numbers, which can be useful when generating readable, shareable, or store unique identifiers. Based on JavaScript, it’s been adapted to Node.js for server-side encoding and decoding of hashes. Unlike many hashing functions, Hashids is meant for non-cryptographic purposes, focusing on obfuscation of identifiers rather than security.
Installation
To begin using Hashids or sqids in your Node.js application, you’ll first need to install it via npm (Node Package Manager):
npm install hashids or npm install squids
Basic Syntax
The basic usage of Node.js Hashids involves creating an instance of Hashids and then calling methods to encode or decode your numbers:
const Hashids = require('hashids/cjs'); const hashids = new Hashids('this is my salt'); const id = hashids.encode(1, 2, 3); const numbers = hashids.decode(id);
The code above initializes Hashids with a salt – a unique string that personalizes the hash outputs. The ‘encode’ method then generates a hash from the provided numbers, and ‘decode’ reverts the hash back to the original numbers.
For Sqids:
Usage of Squids is similar to Hashids, there is not much difference.
const Blob =require('buffer').Blob; //Need to load Blob first const Sqids = require('sqids/cjs').default; const sqids = new Sqids() const id = sqids.encode([1, 2, 3]); //Need to pass array. const numbers = sqids.decode(id);
Practical Examples
Let’s explore some practical examples of Node.js Hashids in action:
Example 1: Basic Encoding and Decoding
const hashids = new Hashids('mySuperSecretSalt'); const myHash = hashids.encode(12345); console.log(`Encoded: ${myHash}`); // Output: Encoded: NkK9 const originalValue = hashids.decode(myHash); console.log(`Decoded: ${originalValue}`); // Output: Decoded: 12345
Example 2: Custom Hash Length
const hashids = new Hashids('mySuperSecretSalt', 8); const myHash = hashids.encode(12345); console.log(`Encoded: ${myHash}`); // Output: Encoded: aBc123eF
Here, we specify a minimum hash length of 8 characters for the output.
Example 3: Encoding Multiple Numbers
const hashids = new Hashids('mySuperSecretSalt'); const myHash = hashids.encode(1, 2, 3); console.log(`Encoded: ${myHash}`); // Output: Encoded: laHquq const originalValues = hashids.decode(myHash); console.log(`Decoded: ${originalValues}`); // Output: Decoded: 1,2,3
In this example, Hashids encodes a sequence of numbers into a single hash.
Example 4: Hexadecimal Encoding and Decoding
const hashids = new Hashids('mySuperSecretSalt'); const myHash = hashids.encodeHex('1a2b3c'); console.log(`Encoded Hex: ${myHash}`); // Output: Encoded Hex: y42lW const originalHex = hashids.decodeHex(myHash); console.log(`Decoded Hex: ${originalHex}`); // Output: Decoded Hex: 1a2b3c
In this instance, we’re working with hexadecimal values.
Further Resources
Conclusive Summary
In conclusion, Hashids is a powerful library for generating short, non-sequential hashes from numbers. It’s particularly useful for encoding database IDs into shorter hashes that are user-friendly and more secure when exposed in URLs. We looked at how to install the library, encode and decode single or multiple numbers and even hexadecimals. With these examples and the Hashids library, you can begin implementing your own hash-based systems in your Node.js applications.