Blog Logo
TAGS

# Hashids

A small .NET package to generate YouTube-like 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, hiding actual IDs, or simply using them as short string IDs. [http://www.hashids.org/net/](http://www.hashids.org/net/) Features Creates short unique IDs from integers. _(only positive numbers & zero)_ Generates non-sequential IDs for incremental input to stay unguessable. Supports a single number or array of numbers. _(supports `int` and `long`)_ Supports custom alphabet and salt — so IDs are unique to your application. _(salt must be smaller than alphabet)_ Supports minimum hash length. Tries to avoid basic English curse words. Notes This is **NOT** a true cryptographic hash, since it is reversible. Only zero and positive integers are supported. Negative numbers will not be encoded. Only a minimum hash length can be specified. There is no way to fit arbitrary numbers within a maximum hash length. The alphabet must contain at least 16 unique characters and is case-sensitive. Separators are characters used to encode multiple numbers in a hash and must also be in the alphabet. The salt must be smaller than the available alphabet and is limited to the length of the `alphabet - separators - 1`. Installation Install the package with [NuGet][] Install-Package hashids.net Usage Import namespace ```C# using HashidsNet; ``` Encoding one number You can pass a unique salt value so your hashes differ from everyone elses. I use **this is my salt** as an example. ```C# var hashids = new Hashids(this is my salt); var hash = hashids.Encode(12345); ``` `hash` is now going to be: NkK9 If your id is stored as a `Int64` you need to use EncodeLong. ```C# var hashids = new Hashids(this is my salt); var hash = hashids.EncodeLong(666555444333222L); ``` `hash` is now going to be: KVO9yy1oO5j Decoding Notice during decoding, same salt value is used: ```C# var hashids = new Hashids(this is my salt); numbers = hashids.Decode(NkK9); ``` `numbers` is now going to be: [ 12345 ] ```C# var hashids = new Hashids(this is my salt); numbers = hashids.DecodeLong(KVO9yy1oO5j); ``` `numbers` is now going to be: [ 666555444333222L ] Decoding a single id By default, Decode and DecodeLong will return an array. If you need to decode just one id you can use the following helper functions: ```C# var hashids = new Hashids(this is my pepper); number = hashids.DecodeSingle(NkK9); ``` `number` is now going to be: 12345 ```C# var hashids = new Hashids(this is my pepper); if (hashids.TryDecodeSingle(NkK9, out int number)) { // Decoding hash successfull. } ``` `number` is now going to be: 12345 You can handle the exception to see what went wrong with the decoding: ```C# var hashids = new Hashids(this is my pepper); try { number = hashids.DecodeSingle(NkK9); } catch (NoResultException) { // Decoding the provided hash has not yielded any result. } ``` `number` is now going to be: 12345 ```C# var hashids = new Hashids(this is my pepper); number = hashids.DecodeSingleLong(KVO9yy1oO5j); ``` `number` is now going to be: 666555444333222L ```C# var hashids = new Hashids(this is my pepper); if (hashids.TryDecodeSingleLong(NkK9, out long number)) { // Decoding hash successfull. }