<p><span style="font-size: 18px;"></span></p>

当你想隐藏数据库id时,你能够使用 Hashids 这个开源库,类似的开源项目比較多,这里仅仅针对 Hashids 做个使用说明

.net  版本号的资料地址例如以下:

官网:http://hashids.org/net/

代码:https://github.com/ullmark/hashids.net

下面是用法:

using System;
using HashidsNet;
using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HashidsNetTester
{
[TestClass]
public class HashidsTest
{
/// <summary>
/// 盐
/// </summary>
private string salt = "this is my salt string";
[TestMethod]
public void TestEncode()
{
//使用指定掩码,获取最小长度的hash值
Hashids hs = new Hashids(salt,5);
var n1 = 1;
var s1 = hs.Encode(n1);//编码:一个数值进行
var dn1 = hs.Decode(s1)[0];//解码:一个数值
Assert.IsTrue(dn1 == n1);
}
[TestMethod]
public void TestEncodeArr()
{
Hashids hs = new Hashids(salt, 5);
var n1 = new int[]{11,22,33,44};
var s1 = hs.Encode(n1);//编码多个数值组,用于数据库中联合主键
var dn1 = hs.Decode(s1);//解码:获得编码前的联合主键数组
for (int i = 0; i < n1.Length; i++)
{
Assert.IsTrue(dn1[i] == n1[i]);
}
}
[TestMethod]
public void TestEncodeHex()
{
Hashids hs = new Hashids(salt, 5);
var n1 = "1abc";
var s1 = hs.EncodeHex(n1);//编码:16进制数
var dn1 = hs.DecodeHex(s1);//解码:获得16进制数
Assert.IsTrue(n1.Equals(dn1, StringComparison.CurrentCultureIgnoreCase));
}
}
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

最新文章

  1. tomcat 7 WARNING: A context path must either be an empty string or start with a &#39;/&#39; and do not end with a &#39;/&#39;. The path [/] does not meet these criteria and has been changed to []
  2. SQL数据库分配权限
  3. MySQL运行状态show status详解
  4. Subtitute
  5. cacti结合nagios
  6. 关于SQL Cookbook里dept与emp表结构以及数据
  7. 在Asp.Net MVC中设定site路径所对应的默认action
  8. 迅为4412开发板Linux驱动教程之GPIO的初始化
  9. The Reflection And Amplification Attacks &amp;&amp; NTP Reply Flood Attack Based On NTP
  10. oracle中用SQL实现两个日期间的日期形成一个数据集
  11. Android 查看webview里面的图片
  12. sublime mac快捷键
  13. OpenCV - Operations on Arrays 对数组(矩阵)的一些操作
  14. PHP运行出现Notice : Use of undefined constant 的解决方法【已测】
  15. 让IE6,7,8支持HTML5新标签的方法
  16. kill -QUIT &lt;pid&gt;
  17. python学习之路前端-CSS
  18. Win10构建Python全栈开发环境With WSL
  19. 新鲜出炉的一套Java面试题
  20. 喵哈哈村的魔法考试 Round #8 (Div.2) 题解

热门文章

  1. 如何使用google地图的api(整理)
  2. 12.1、USB驱动——描述符、URB、管道
  3. linux系统进程的查看与控制
  4. 2015第30周四Java日志组件
  5. Tidhy
  6. ViewChild与ContentChild的联系和区别
  7. ZOJ FatMouse&#39; Trade 贪心
  8. 计算git树上随意两点的近期切割点。
  9. C# 二分法查找和排序
  10. TF-IDF模型