hashid — Generate short unique ids from integers
Date: 2022-12-30Last modified: 2024-11-02
Table of contents
Features
- Generate short unique ids from integers
- Create short unique ids from numbers (positive numbers & zero).
- Allow custom alphabet as well as salt — so ids are unique only to you.
- Incremental input is mangled to stay unguessable.
- Code is tiny (~350 lines), fast and does not depend on external libraries.
- Single C++ header https://raw.githubusercontent.com/schoentoon/hashidsxx/master/hashids.h
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