asp.net中如何编写hash函数计算中文的hash值使之生成二进制数01
产生16进制数,每一位都可以查表得到特定的4位二进制数。
“使之生成二进制数01”是什么意思?一般来说有中文的固定编码和hash函数就可以生成hash了
给你个样子吧
using System;
using System.Security.Cryptography;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
//读取数据
var s = Console.ReadLine();
//中文解码
var b = Console.InputEncoding.GetBytes(s);
//Hash
MD5CryptoServiceProvider mp = new MD5CryptoServiceProvider();
var h = mp.ComputeHash(b);
//数值转换
var binarys = new byte[h.Length * 8];
for (int i = 0; i < h.Length; i++)
{
binarys[i * 8] = (byte)(h[i] >> 7);
binarys[i * 8 + 1] = (byte)((h[i] >> 6) & 0x01);
binarys[i * 8 + 2] = (byte)((h[i] >> 5) & 0x01);
binarys[i * 8 + 3] = (byte)((h[i] >> 4) & 0x01);
binarys[i * 8 + 4] = (byte)((h[i] >> 3) & 0x01);
binarys[i * 8 + 5] = (byte)((h[i] >> 2) & 0x01);
binarys[i * 8 + 6] = (byte)((h[i] >> 1) & 0x01);
binarys[i * 8 + 7] = (byte)(h[i] & 0x01);
}
//输出
Console.Write("The result is :");
for (int i = 0; i < binarys.Length; i++)
{
if (i % 8 == 0) Console.Write(" ");
Console.Write(binarys[i]);
}
Console.WriteLine();
Console.ReadLine();
}
}
}