Hashids.net 1.6.1

.NET 5.0 .NET Standard 2.0
NuGet\Install-Package Hashids.net -Version 1.6.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package Hashids.net --version 1.6.1
<PackageReference Include="Hashids.net" Version="1.6.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hashids.net --version 1.6.1
#r "nuget: Hashids.net, 1.6.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hashids.net as a Cake Addin
#addin nuget:?package=Hashids.net&version=1.6.1

// Install Hashids.net as a Cake Tool
#tool nuget:?package=Hashids.net&version=1.6.1

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/

Features

  • Creates short unique ids from integers. (only positive numbers & zero)
  • Generates non-sequential IDs for incremental input to stay unguessable.
  • Supports single number or array of numbers. (supports int and long)
  • Allows custom alphabet as well as salt — so ids are unique only to you. (salt must be smaller than alphabet)
  • Allows specifying minimum hash length.
  • Tries to avoid basic English curse words.

NOTE: This is NOT a true cryptographic hash, since it is reversible

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.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 alphabet (dashes caused issues with Regex which is not used anymore).
  • Issue #21 - Fix encoding exception when decoding a character used as guard.
  • Issue #29 - Added tests to confirm thread-safety.

1.3.0

  • PR #26 - Support .netstandard2.0.

1.2.2

  • PR #19 - Only instantiate the HEX-connected Regexes if we use any of the HEX functions. This will speed up creation of "Hashids"-instances. It is likely that most users doesn't use the HEX-functions.

1.2.1

  • PR #11 - Speed up consistent shuffle with less string manipulation.
  • Issue #15 - Decoding strings that contain characters not in the alphabet will now return empty array. (To conform to behaviour in the js-library).
  • Issue #18 - Encoding with a negative number will now return empty string. (To conform to behaviour in the js-library).

1.2.0Added

  • Added .NET Core support.

1.1.2

  • Fixed issue #14 that caused HEX values to be encoded/decoded incorrectly.

1.1.1

  • Accepted PR #12 that fixed an issue when encoding very many longs at the same time

1.1.0

  • Added support for long via new functions to not introduce breaking changes.
    • EncodeLong for encodes.
    • DecodeLong for decodes.
  • Added interface IHashids for people who want an interface to work with.

1.0.1

  • The .NET 4.0 version of the package used .NET 4.5 as build target. This was fixed and a new version was pushed to nuget.

1.0.0

  • Several public functions marked obsolete and renamed versions added, to be more appropriate:

    • Function Encrypt() changed to Encode()
    • Function Decrypt() changed to Decode()
    • Function EncryptHex() changed to EncodeHex()
    • Function DecryptHex() changed to DecodeHex()

    Hashids was designed to encode integers, primary ids at most. We've had several requests to encrypt sensitive data with Hashids and this is the wrong algorithm for that. So to encourage more appropriate use, encrypt/decrypt is being "downgraded" to encode/decode.

0.3.4

  • The public functions are now virtual and therefor can be mocked with a mocking library.

0.3.3

  • Rewrote the code to support the new hashing algorithm.
  • Support for EncryptHex and DecryptHex

0.1.4

  • Initial version of the port.
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (23)

Showing the top 5 NuGet packages that depend on Hashids.net:

Package Downloads
Apprio.Enablement.Platform

Package Description

Apprio.Enablement.Business.Encounters

Package Description

PackUtils

Many utilities like: enumeration, hash, json (extension newtonsoft), regex, signature, string, uri, dictionary, etc

SFA.DAS.Encoding

Encode and Decode sensitive values, reads parameters from configuration.

AspNetCore.Hashids

Not predictable ids library for ASP.NET Core APIs. For more information see http://github.com/Xabaril/AspNetCore.Hashids

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on Hashids.net:

Repository Stars
SparkDevNetwork/Rock
An open source CMS, Relationship Management System (RMS) and Church Management System (ChMS) all rolled into one.
Anapher/Strive
Open source video conferencing platform
neozhu/visitormanagement
helps in managing visitors visiting the institutions for various reasons. It allows visitors to check-in digitally to eliminate the tedious registeration and other paperwork. Additionally, it also keeps a track of every individual inside the campus and their timings. Institutions has guards who enter their detail in some notebooks to keep a log which are practically impossible to reconcile. It is really unpleasent and hectic for visitor to stand at the gate and give details about the visit. To ease the process of registeration, Entry-In, Entry-Out, time tracking and logging the history, this VMS can be of great use!!
Version Downloads Last updated
1.6.1 134,487 6/2/2022
1.6.0 18,160 5/22/2022
1.5.0 82,952 4/2/2022
1.4.1 355,956 7/3/2021
1.4.0 98,000 5/4/2021
1.3.0 1,197,209 10/29/2019
1.2.2 1,580,327 9/14/2016
1.2.1 7,019 9/10/2016
1.2.0 9,688 9/2/2016
1.2.0-rc 1,240 9/2/2016
1.1.2 64,692 9/9/2015
1.1.1 7,351 8/29/2015
1.1.0 4,871 6/29/2015
1.0.1 116,499 4/25/2015
1.0.0 10,107 9/14/2014
0.3.4 1,038 9/14/2014
0.3.3 1,475 4/22/2014
0.1.4 1,758 12/20/2012