Back to TILs

hashid — Generate short unique ids from integers

Date: 2022-12-30Last modified: 2022-12-31

Table of contents

Features

Basic usage

Encoding

    // 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

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);

Custom salts, alphabets and minimum lengths

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

Possible output


References