Generate short unique ids from integers
available in JavaScript, Ruby, Python, Java, Scala, PHP, Perl, Perl 6, Swift, Clojure, Objective-C, C, C++11, D, F#, Go, Erlang, Lua, Haskell, OCaml, Elixir, Rust, Smalltalk, ColdFusion, Kotlin, Nim, VBA, Haxe, Crystal, Elm, ActionScript, Bash, R, TSQL, PostgreSQL, PLpgSQL, Dart, Io, Julia and for .NET
Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers.
It converts numbers like 347 into strings like “yr8”, or array of numbers like [27, 986] into “3kTMd”.
You can also decode those ids back. This is useful in bundling several parameters into one or simply using them as short UIDs.
Are you also using Hashids? Add your project.
var hashids:Hashids = new Hashids("this is my salt");
var id:String = hashids.encode(1, 2, 3);
var numbers:Vector.<Number> = hashids.decode(id);
Implemented by Jovan Nikolić — https://github.com/jovanpn/hashids.as
Hashids works similarly to the way integers are converted to hex, but with a few exceptions:
This JavaScript function shows regular conversion of integers to hex. It's part of Hashids (although this is a modified example):
JavaScriptfunction toHex(input) {
var hash = "",
alphabet = "0123456789abcdef",
alphabetLength = alphabet.length;
do {
hash = alphabet[input % alphabetLength] + hash;
input = parseInt(input / alphabetLength, 10);
} while (input);
return hash;
}
We need ids to be nice and friendly especially if they end up being in the URL.
Therefore, the algorithm tries to avoid generating most common English curse words by never placing the following letters (and their uppercase equivalents) next to each other:
c, s, f, h, u, i, t
This is done by using them as separators.
Yes, there are a few and you should pick the most appropriate for the job. Hashids is not perfect for everything.
There are no collisions because the method is based on integer to hex conversion. As long as you don't change constructor arguments midway, the generated output will stay unique to your salt.
Additionally, we encourage you to pre-generate a large number of ids for testing purposes — to analyze their uniqueness, whether they look random enough for your project and what your upper integer limit is (which usually depends on your system/language).
Please note that generated ids are case-sensitive by default ("Aaa" is not the same id as "aaa").
Originally the project referred to generated ids as hashes, and obviously the name hashids has the word hash in it. Technically, these generated ids cannot be called hashes since a cryptographic hash has one-way mapping (cannot be decrypted).
However, when people search for a solution, like a "youtube hash" or "bitly short id", they usually don't really care of the technical details. So hashids stuck as a term — an algorithm to obfuscate numbers.
Do you have a question or comment that involves "security" and "hashids" in the same sentence? Don't use Hashids. Here are some ways to decode:
If you've found a bug, please open a github issue in the appropriate repository. Bonus points if you submit a pull request with it.
If an implementation in your favorite language is missing, feel free to port it over from one of the existing versions. There's still plenty of languages to contribute in: Nu, Groovy, Racket, Scheme, Tcl, Dylan, Lolcode, Factor?
We try to keep the library versions compatible. If you see an outdated version in an existing implementation, please open a github issue in that repository — show your +1 support for that issue.
If you have a question about Hashids, a lot has already been answered. Try checking one of these:
Many thanks to the numerous folks that have implemented the different versions, plugins, extensions as well as to those that have suggested general improvements.