SkillAgentSearch skills...

Hashids.net

A small .NET package to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.

Install / Use

/learn @ullmark/Hashids.net
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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/

NOTE: You might want to use sqids-dotnet instead.

The original author of the Hashids algoritm has rebranded and created a new algoritm called "sqids", you can read more about why here.. A .NET version of sqids has already been created and can be found here.

This library have more or less been considered feature complete since its creation 2012 and will remain available and accept pull requests for bug fixes etc. but no new features will be added.

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

using HashidsNet;

Encoding one number

You can pass a unique salt value so your hashes differ from everyone else's. I use "this is my salt" as an example.

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".

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:

var hashids = new Hashids("this is my salt");
numbers = hashids.Decode("NkK9");

numbers is now going to be:

[ 12345 ]
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:

var hashids = new Hashids("this is my pepper");
number = hashids.DecodeSingle("NkK9");

number is now going to be:

12345
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:

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
var hashids = new Hashids("this is my pepper");
number = hashids.DecodeSingleLong("KVO9yy1oO5j");

number is now going to be:

666555444333222L
var hashids = new Hashids("this is my pepper");

if (hashids.TryDecodeSingleLong("NkK9", out long number)) { // Decoding hash successfull. }

number is now going to be:

666555444333222L
var hashids = new Hashids("this is my pepper");
try
{
    number = hashids.DecodeSingleLong("KVO9yy1oO5j");
}
catch (NoResultException) { // Decoding the provided hash has not yielded any result. }

number is now going to be:

666555444333222L

Decoding with different salt

Decoding will not work if salt is changed:

var hashids = new Hashids("this is my pepper");
numbers = hashids.Decode("NkK9");

numbers is now going to be:

[]

Encoding several numbers

var hashids = new Hashids("this is my salt");
var hash = hashids.Encode(683, 94108, 123, 5);

hash is now going to be:

aBMswoO2UB3Sj

Decoding is done the same way

var hashids = new Hashids("this is my salt");
var numbers = hashids.Decode("aBMswoO2UB3Sj")

numbers is now going to be:

[ 683, 94108, 123, 5 ]

Encoding and specifying minimum hash length

Here we encode integer 1, and set the minimum hash length to 8 (by default it's 0 -- meaning hashes will be the shortest possible length).

var hashids = new Hashids("this is my salt", 8);
var hash = hashids.Encode(1);

hash is now going to be:

gB0NV05e

Decoding

var hashids = new Hashids("this is my salt", 8);
var numbers = hashids.Decode("gB0NV05e");

numbers is now going to be:

[ 1 ]

Specifying custom hash alphabet

Here we set the alphabet to consist of: "abcdefghijkABCDEFGHIJK12345"

var hashids = new Hashids("this is my salt", 0, "abcdefghijkABCDEFGHIJK12345")
var hash = hashids.Encode(1, 2, 3, 4, 5)

hash is now going to be:

Ec4iEHeF3

Randomness

The primary purpose of hashids is to obfuscate ids. It's not meant or tested to be used for security purposes or compression. Having said that, this algorithm does try to make these hashes unguessable and unpredictable:

Repeating numbers

var hashids = new Hashids("this is my salt");
var hash = hashids.Encode(5, 5, 5, 5);

You don't see any repeating patterns that might show there's 4 identical numbers in the hash:

1Wc8cwcE

Same with incremented numbers:

var hashids = new Hashids("this is my salt");
var hash = hashids.Encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

hash will be :

kRHnurhptKcjIDTWC3sx

Incrementing number hashes:

var hashids = new Hashids("this is my salt");

hashids.Encode(1); // => NV
hashids.Encode(2); // => 6m
hashids.Encode(3); // => yD
hashids.Encode(4); // => 2l
hashids.Encode(5); // => rD

Encoding using a HEX string

var hashids = new Hashids("this is my salt");
var hash = hashids.EncodeHex("DEADBEEF");

hash is now going to be:

kRNrpKlJ

Decoding to a HEX string

var hashids = new Hashids("this is my salt");
var hex = hashids.DecodeHex("kRNrpKlJ");

hex is now going to be:

DEADBEEF

Changelog

v.1.7.0

  • PR #86 - Fix for decoding hash smaller than min length setting.
  • Project build targets now set to netstandard2.0, net6.0, net7.0.

v.1.6.1

  • PR #76 - Fix min buffer sizes.

v.1.6.0

  • PR #66 - Fixed invalid constant bug.
  • PR #67 and PR #73 - Update and cleanup tests and constants.
  • PR #65 - Improved performance and reduced allocations for single number decode.

v1.5.0

  • PR #59 and PR #61 - Project clean up and removal of net461 target.
  • PR #50 - Added support for .NET 6.
  • PR #49 - Optimized methods for single number encoding.
  • PR #57 - Optimized methods for single number decoding.
  • PR #54 and #58 - Fixed Github Actions build and test.
  • PR #55 - Removed System.Buffers dependency for .NET 5 and higher.
  • PR #47 - Improved performance with readonly and Span<T> usage.
  • PR #60 - Reference System.Memory to replace internal ReadOnlySpan<T> class.
  • PR #63 - Array and Span usage optimizations.
  • PR #62 - Documentation improvements.

1.4.1

  • PR #45 - Cleanup unused nuget references and replace Microsoft.Extensions.ObjectPool with internal implementation.

1.4.0

  • Modernized project with updated build targets now set to net461, net5.0, netstandard2.0
  • PR #30 - Fix floating-point math to handle large ratio of alphabet to separators.
  • PR #37 - Performance and memory optimizations.
  • PR #42 - Performance updates and added BenchmarkDotnet for profiling.
  • PR #43 - Improved performance and reduced allocations.
  • Issues #23, #32, #35 - Fix floating-point math, now replaced by Horner's method.
  • Issue #27 - Allow dashes in alpha

Related Skills

View on GitHub
GitHub Stars3.4k
CategoryData
Updated21h ago
Forks175

Languages

C#

Security Score

100/100

Audited on Mar 29, 2026

No findings