闽公网安备 35020302035485号
5.允许指定最小哈希长度
Install-Package Hashids.net2.NuGet程序包管理工具


using HashidsNet;编码单个数字
// 堆代码 duidaima.com
var Hashids = new Hashids("this is my salt");
var hash = hashids.Encode(12345);
运行结果为:NkK9var hashids = new Hashids("this is my salt");
var hash = hashids.EncodeLong(666555444333222L);
运行结果为:KVO9yy1oO5jvar hashids = new Hashids("this is my salt");
numbers = hashids.Decode("NkK9");
运行结果为:[ 12345 ]var hashids = new Hashids("this is my salt");
numbers = hashids.DecodeLong("KVO9yy1oO5j");
运行结果为:[ 666555444333222L ]var hashids = new Hashids("this is my pepper");
numbers = hashids.Decode("NkK9");
运行结果为:[]var hashids = new Hashids("this is my salt");
var hash = hashids.Encode(683, 94108, 123, 5);
运行结果为:aBMswoO2UB3Sjvar hashids = new Hashids("this is my salt");
var numbers = hashids.Decode("aBMswoO2UB3Sj")
运行结果为:[ 683, 94108, 123, 5 ]var hashids = new Hashids("this is my salt", 8);
var hash = hashids.Encode(1);
运行结果为:gB0NV05evar hashids = new Hashids("this is my salt", 8);
var numbers = hashids.Decode("gB0NV05e");
运行结果为:[ 1 ]var hashids = new Hashids("this is my salt", 0, "abcdefghijkABCDEFGHIJK12345")
var hash = hashids.Encode(1, 2, 3, 4, 5)
运行结果为:Ec4iEHeF3var hashids = new Hashids("this is my salt");
var hash = hashids.Encode(5, 5, 5, 5);
编码后,你不会看到任何重复的模式来表明哈希中有4个相同的数字,运行结果为:1Wc8cwcE。var hashids = new Hashids("this is my salt");
var hash = hashids.Encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
运行结果为:kRHnurhptKcjIDTWC3sxvar 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
编码十六进制var hashids = new Hashids("this is my salt");
var hash = hashids.EncodeHex("DEADBEEF");
运行结果为:kRNrpKlJvar hashids = new Hashids("this is my salt");
var hex = hashids.DecodeHex("kRNrpKlJ");
运行结果为:DEADBEEFpublic static string GetHashids(this string sourceStr, int number = 9)
{
var hashids = new Hashids(sourceStr);
return hashids.Encode(number);
}
单元测试如下:比如将本文别名【Is-it-possible-to-use-it-as-a-short-link-generator-Hashidsnet】加密[TestClass]
public class HashHelperUnitTest
{
[TestMethod]
public void Hashids_Success()
{
var blogPostSlugStr = "Is-it-possible-to-use-it-as-a-short-link-generator-Hashidsnet";
var encodeStr1 = blogPostSlugStr.GetHashids();
var encodeStr2 = blogPostSlugStr.GetHashids();
Assert.AreEqual(encodeStr1, encodeStr2);
}
}
别名加密后为:【6Q】,可打开浏览器访问本文短链接地址尝试:https://dotnet9.com/6Q 。public static string ShortenString(string longString)
{
// 堆代码 duidaima.com
byte[] bytes = Encoding.UTF8.GetBytes(longString);
string shortString = Convert.ToBase64String(bytes);
return shortString;
}
public static string RestoreString(string shortString)
{
byte[] bytes = Convert.FromBase64String(shortString);
string longString = Encoding.UTF8.GetString(bytes);
return longString;
}
不可逆算法示例(C#):public static string ShortenString(string longString)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(longString));
string shortString = Convert.ToBase64String(hashBytes);
return shortString;
}
}
区别:using HashidsNet;
using System.Security.Cryptography;
using System.Text;
namespace Dotnet9.Commons.Test;
[TestClass]
public class HashHelperUnitTest
{
public static string GetHashids(string sourceStr, int number = 9)
{
var hashids = new Hashids(sourceStr);
return hashids.Encode(number);
}
[TestMethod]
public void Hashids_Success()
{
var blogPostSlugStr = "Is-it-possible-to-use-it-as-a-short-link-generator-Hashidsnet";
var encodeStr1 = GetHashids(blogPostSlugStr);
var encodeStr2 = GetHashids(blogPostSlugStr);
Assert.AreEqual(encodeStr1, encodeStr2);
}
[TestMethod]
public void Hashids_Best_Success()
{
var blogPostSlugStr = "Is-it-possible-to-use-it-as-a-short-link-generator-Hashidsnet";
var encodeStr1 = blogPostSlugStr.GetHashids();
var encodeStr2 = ShortenString(blogPostSlugStr);
var encodeStr3 = ShortenString2(blogPostSlugStr);
Assert.IsTrue(encodeStr1.Length < encodeStr2.Length, "Hashids生成的短字符串比Base64还短");
Assert.IsTrue(encodeStr1.Length < encodeStr3.Length, "Hashids生成的短字符串还是短那么一点点");
}
public static string ShortenString(string longString)
{
byte[] bytes = Encoding.UTF8.GetBytes(longString);
string shortString = Convert.ToBase64String(bytes);
return shortString;
}
public static string RestoreString(string shortString)
{
byte[] bytes = Convert.FromBase64String(shortString);
string longString = Encoding.UTF8.GetString(bytes);
return longString;
}
public static string ShortenString2(string longString)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(longString));
string shortString = Convert.ToBase64String(hashBytes);
return shortString;
}
}
}