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.
hashids (Hash ID's) creates short, unique, decryptable hashes from unsigned integers.
(NOTE: This is NOT a true cryptographic hash, since it is reversible)
It was designed for websites to use in URL shortening, tracking stuff, or making pages private (or at least unguessable).
This algorithm tries to satisfy the following requirements:
- Hashes must be unique and decryptable.
- They should be able to contain more than one integer (so you can use them in complex or clustered systems).
- You should be able to specify minimum hash length.
- Hashes should not contain basic English curse words (since they are meant to appear in public places - like the URL).
Instead of showing items as 1
, 2
, or 3
, you could show them as U6dc
, u87U
, and HMou
.
You don't have to store these hashes in the database, but can encrypt + decrypt on the fly.
All integers need to be greater than or equal to zero.
Install the package with NuGet
Install-Package hashids.net
using HashidsNet;
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.Encrypt(12345);
hash
is now going to be:
NkK9
Notice during decryption, same salt value is used:
var hashids = new Hashids("this is my salt");
numbers = hashids.Decrypt("NkK9");
numbers
is now going to be:
[ 12345 ]
Decryption will not work if salt is changed:
var hashids = new Hashids("this is my pepper");
numbers = hashids.Decrypt("NkK9");
numbers
is now going to be:
[]
var hashids = new Hashids("this is my salt");
var hash = hashids.Encrypt(683, 94108, 123, 5);
hash
is now going to be:
aBMswoO2UB3Sj
var hashids = new Hashids("this is my salt");
var numbers = hashids.Decrypt("aBMswoO2UB3Sj")
numbers
is now going to be:
[ 683, 94108, 123, 5 ]
Here we encrypt 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.Encrypt(1);
hash
is now going to be:
gB0NV05e
var hashids = new Hashids("this is my salt", 8);
var numbers = hashids.Decrypt("gB0NV05e");
numbers
is now going to be:
[ 1 ]
Here we set the alphabet to consist of: "abcdefghijkABCDEFGHIJK12345"
var hashids = new Hashids("this is my salt", 0, "abcdefghijkABCDEFGHIJK12345")
var hash = hashids.Encrypt(1, 2, 3, 4, 5)
hash
is now going to be:
Ec4iEHeF3
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:
var hashids = new Hashids("this is my salt");
var hash = hashids.Encrypt(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.Encrypt(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
hash
will be :
kRHnurhptKcjIDTWC3sx
var hashids = new Hashids("this is my salt");
hashids.Encrypt(1); // => NV
hashids.Encrypt(2); // => 6m
hashids.Encrypt(3); // => yD
hashids.Encrypt(4); // => 2l
hashids.Encrypt(5); // => rD
var hashids = new Hashids("this is my salt");
var hash = hashids.EncryptHex("DEADBEEF");
hash
is now going to be:
kRNrpKlJ
var hashids = new Hashids("this is my salt");
var hex = hashids.DecryptHex("kRNrpKlJ");
hex
is now going to be:
DEADBEEF
1.0.0
-
Several public functions marked obsolete and renamed versions added, to be more appropriate:
- Function
Encrypt()
changed toEncode()
- Function
Decrypt()
changed toDecode()
- Function
EncryptHex()
changed toEncodeHex()
- Function
DecryptHex()
changed toDecodeHex()
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" toencode/decode
. - Function
-
Version tag added:
1.0
-
README.md
updated
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
andDecryptHex
0.1.4
- Initial version of the port.