Back to TILs

hashid — Generate short unique ids from integers

Date: 2022-12-30Last modified: 2023-03-16

Table of contents

Features

Basic usage

Encoding a single number

    hashidsxx::Hashids hash;
    std::cout << hash.encode({123}) << std::endl;  // Mj3

Decoding

    std::vector<uint64_t> output = hash.decode("Mj3");
    for (uint64_t h : output) {
      std::cout << h << std::endl;  // 123
    }

Encoding several numbers with salt

    hashidsxx::Hashids hash("this is my salt");
    std::string id = hash.encode({1, 2, 3});
    std::vector<uint64_t> numbers = hash.decode(id);
    for (auto &h : numbers) {
      std::cout << h << std::endl;
    }

Custom salts, alphabets and minimum lengths

    hashidsxx::Hashids hash("salt", 16, "abcdefghijklmnopqrstuvwxyz");
    std::cout << hash.encode({123456789}) << std::endl;  // oavlpogkzrxrkpxd

Possible output

Mj3
123
1
2
3
oavlpogkzrxrkpxd

References