Net Core进阶,MD5最优打开方式

Net Core进阶,MD5最优打开方式
2019年10月22日 11:20 互联汪

可以支持 SHA1,SHA256,.Net Framework.

public static string GetMd5Hash(string input)

{

using (MD5 md5Hash = MD5.Create())

{

// Convert the input string to a byte array and compute the hash.

byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes

// and create a string.

StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data

// and format each one as a hexadecimal string.

for (int i = 0; i

{

sBuilder.Append(data[i].ToString("x2"));

}

// Return the hexadecimal string.

return sBuilder.ToString();

}

}

这是一段 MSDN 官方的 MD5 示例,例子很简单且很容易理解。但是,这个例子也有很多的问题,首先上例至少创建了 3 个临时缓存区!且每次执行 GetMd5Hash 都会创建一个 MD5 实例,并在方法执行完成后释放它。这些都造成了很大的系统资源浪费和增加了 GC 的压力。

鉴于官方给的 Demo 并不优秀,且网上也没有给出很好使用方式,这里我就拿出我多年使用的 MD5 打开方式,这个方法同时支持 SHA1,SHA256 等,即支持 System.Security.Cryptography 命名空间下的 HashAlgorithm(哈希算法) 实现。也同时支持 .Net Framework 2.0 之后的所有 .Net 平台。

我不想看你的鬼废话,直接给我上最终代码》》》

先说明,这个文章是基于 System.Security.Cryptography 命名空间的实现,不是自己写一个 MD5 算法哦。

using System;

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Security.Cryptography;

static class THashAlgorithmInstances

where THashAlgorithm : HashAlgorithm

{

///

/// 线程静态变量。

/// 即:这个变量在每个线程中都是唯一的。

/// 再结合泛型类实现:该变量在不同泛型或不同的线程下的值都是不一样的。

/// 这样做的目的是为了避开多线程问题。

/// 关于垃圾回收:当 .NET 线程被释放时,程序中的所有线程静态变量都会被回收,GC 回收时同时将释放资源,所以不必担心释放问题,GC 会帮助我们的。

/// 这里描述的 .NET 线程释放不是指 .NET 线程回收至线程池。很多时候 .NET 的线程在程序关闭之前都不会真正释放,而是在线程池中继续驻留。

/// 线程唯一真的能避免多线程问题吗?答:多个线程所以用存储空间都不一样,那么脏值就不可能存在,如果这都能出现多线程问题,我直播吃....猪红(本人及其厌恶吃猪红

现在我们开始,首先我们先定义一个辅助类:

该辅助类帮助我们避开多线程问题,且帮助我们创建指定的 HashAlgorithm 实例。

这里说明一下,HashAlgorithm.ComputeHash (同 MD5.ComputeHash) 方法绝对不是线程安全的!大家使用它的时候必须要注意,在未线程同步下调用同一实例的 ComputeHash 方法得到的结果是错误的!

public static class HashAlgorithmHelper

{

public static string ComputeHash

(string input) where THashAlgorithm : HashAlgorithm

{

var data = THashAlgorithmInstances

.Instance.ComputeHash(Encoding.UTF8.GetBytes(input));

var sBuilder = new StringBuilder();

foreach (var item in data)

{

sBuilder.Append(item.ToString("x2"));

}

return sBuilder.ToString();

}

}

接下来我们再定义实现类:

到这里我们入门级的 MD5 打开方式就完成了,使用方法:

HashAlgorithmHelper.ComputeHash

("Hello World!")

我们来先测试一下:

static void Main(string[] args)

{

Console.WriteLine(HashAlgorithmHelper.ComputeHash

("Hello World!"));

Console.WriteLine(GetMd5Hash("Hello World!"));

while (true)

{

var stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

HashAlgorithmHelper.ComputeHash

("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

GetMd5Hash("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

}

}

输出结果:

可以看出我们的性能已经超官方 Demo 近一倍了。

接下来我们将进入进阶级打开方式,我们现在需要自己写一个简单的 byte[] To string 方法,我们先打开 C# 项目的 “允许不安全代码” 选项。

在解决方案中右键项目->属性->生成->勾选“允许不安全代码”。

然后我们在 HashAlgorithmHelper 类中定义新的 ToString 方法。

public static string ComputeHash

(string input) where THashAlgorithm : HashAlgorithm

{

var bytes = Encoding.UTF8.GetBytes(input);

var data = THashAlgorithmInstances

.Instance.ComputeHash(bytes);

return ToString(data);

}

然后我们修改 ComputeHash 方法为如下:

现在我们再测试就会发现已经比官方 Demo 快 4 倍了!现在这个 MD5 打开方式已经适合绝大多数人了,如果您不喜欢不安全代码,也可以用数组代替,效率只差一丢丢而已,该方式我会在下方给出完整代码。

接下来我们使用 .Net Core 以最优的方式打开,我们修改 HashAlgorithmHelper 为如下:(这里就不再支持 .Net Framework 了)

public static class HashAlgorithmHelper

{

static readonly char[] Digitals = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

// 在这个函数里要用到的 bytes 成员 ReadOnlySpan

与 byte[] 的一致,所以我们只需要修改参数类型即可。

static string ToString(ReadOnlySpan

bytes)

{

unsafe

{

const int byte_len = 2; // 表示一个 byte 的字符长度。

var str = new string('\0', byte_len * bytes.Length);

fixed(char* pStr = str)

{

var pStr2 = pStr; // fixed pStr 是只读的,所以我们定义一个变量。

foreach (var item in bytes)

{

*pStr2 = Digitals[item >> 4/* byte high */]; ++pStr2;

*pStr2 = Digitals[item & 15/* byte high */]; ++pStr2;

}

}

return str;

}

}

public static string ComputeHash

(string input) where THashAlgorithm : HashAlgorithm

{

var instance = THashAlgorithmInstances

.Instance; // 避免二次取值,微微提高效率(自我感觉)。

var encoding = Encoding.UTF8;

// 我们在这里声明一个足量的 byte 数组,足以容下字符串的 utf-8 字节码和 hash 值的字节码。

var bytes = new byte[Encoding.UTF8.GetMaxByteCount(Math.Max(input.Length, instance.HashSize / 2))];

var bytesCount = encoding.GetBytes(input, bytes);

var source = new ReadOnlySpan

(bytes, 0, bytesCount); // source: utf-8 bytes region.

var destination = new Span

(bytes, bytesCount, bytes.Length - bytesCount); // destination: buffer region.

if (bytes.Length - bytesCount > instance.HashSize && instance.TryComputeHash(source, destination, out var bytesWritten))

{

return ToString(destination.Slice(0, bytesWritten));

}

else

{

// 通常情况下这里就很有可能抛出异常了,但是我们封装工具方法必须有一个原则,我们尽量不要自行抛出。

// 用户的参数执行到这里我们依然调用 HashAlgorithm.ComputeHash,由它内部抛出异常。这样可以避免很多问题和歧义。

return ToString(instance.ComputeHash(bytes, 0, bytesCount));

}

}

}

我们再次测试,结果如下:

现在我们已经超官方示例达 5 倍了!这就是最终版本了。

最后附上各个版本实现的完整代码:

Core 2.1+ 包含不安全代码版本:

using System;

using System.Diagnostics;

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Security.Cryptography;

using System.Text;

static class THashAlgorithmInstances

where THashAlgorithm : HashAlgorithm

{

///

/// 线程静态变量。

/// 即:这个变量在每个线程中都是唯一的。

/// 再结合泛型类实现了该变量在不同泛型或不同的线程先的变量都是唯一的。

/// 这样做的目的是为了避开多线程问题。

///

[ThreadStatic]

static THashAlgorithm instance;

public static THashAlgorithm Instance => instance ?? Create();

///

/// 寻找 THashAlgorithm 类型下的 Create 静态方法,并执行它。

/// 如果没找到,则执行 Activator.CreateInstance 调用构造方法创建实例。

/// 如果 Activator.CreateInstance 方法执行失败,它会抛出异常。

///

[MethodImpl(MethodImplOptions.NoInlining)]

static THashAlgorithm Create()

{

var createMethod = typeof(THashAlgorithm).GetMethod(

nameof(HashAlgorithm.Create),

BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly,

Type.DefaultBinder,

Type.EmptyTypes,

null);

if (createMethod != null)

{

instance = (THashAlgorithm)createMethod.Invoke(null, new object[] { });

}

else

{

instance = Activator.CreateInstance

();

}

return instance;

}

}

public static class HashAlgorithmHelper

{

static readonly char[] Digitals = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

static string ToString(ReadOnlySpan

bytes)

{

unsafe

{

const int byte_len = 2; // 表示一个 byte 的字符长度。

var str = new string('\0', byte_len * bytes.Length);

fixed(char* pStr = str)

{

var pStr2 = pStr; // fixed pStr 是只读的,所以我们定义一个变量。

foreach (var item in bytes)

{

*pStr2 = Digitals[item >> 4/* byte high */]; ++pStr2;

*pStr2 = Digitals[item & 15/* byte high */]; ++pStr2;

}

}

return str;

}

}

public static string ComputeHash

(string input) where THashAlgorithm : HashAlgorithm

{

var instance = THashAlgorithmInstances

.Instance;

var encoding = Encoding.UTF8;

var bytes = new byte[Encoding.UTF8.GetMaxByteCount(Math.Max(input.Length, instance.HashSize / 2))];

var bytesCount = encoding.GetBytes(input, bytes);

var source = new ReadOnlySpan

(bytes, 0, bytesCount); // source: utf-8 bytes region.

var destination = new Span

(bytes, bytesCount, bytes.Length - bytesCount); // destination: buffer region.

if (bytes.Length - bytesCount > instance.HashSize && instance.TryComputeHash(source, destination, out var bytesWritten))

{

return ToString(destination.Slice(0, bytesWritten));

}

else

{

return ToString(instance.ComputeHash(bytes, 0, bytesCount));

}

}

}

class Program

{

public static string GetMd5Hash(string input)

{

using (MD5 md5Hash = MD5.Create())

{

// Convert the input string to a byte array and compute the hash.

byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes

// and create a string.

StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data

// and format each one as a hexadecimal string.

for (int i = 0; i

{

sBuilder.Append(data[i].ToString("x2"));

}

// Return the hexadecimal string.

return sBuilder.ToString();

}

}

static void Main(string[] args)

{

Console.WriteLine(HashAlgorithmHelper.ComputeHash

("Hello World!"));

Console.WriteLine(GetMd5Hash("Hello World!"));

while (true)

{

var stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

HashAlgorithmHelper.ComputeHash

("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

GetMd5Hash("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

}

}

}

包含不安全代码的通用版本:

using System;

using System.Diagnostics;

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Security.Cryptography;

using System.Text;

static class THashAlgorithmInstances

where THashAlgorithm : HashAlgorithm

{

///

/// 线程静态变量。

/// 即:这个变量在每个线程中都是唯一的。

/// 再结合泛型类实现了该变量在不同泛型或不同的线程先的变量都是唯一的。

/// 这样做的目的是为了避开多线程问题。

///

[ThreadStatic]

static THashAlgorithm instance;

public static THashAlgorithm Instance => instance ?? Create();

///

/// 寻找 THashAlgorithm 类型下的 Create 静态方法,并执行它。

/// 如果没找到,则执行 Activator.CreateInstance 调用构造方法创建实例。

/// 如果 Activator.CreateInstance 方法执行失败,它会抛出异常。

///

[MethodImpl(MethodImplOptions.NoInlining)]

static THashAlgorithm Create()

{

var createMethod = typeof(THashAlgorithm).GetMethod(

nameof(HashAlgorithm.Create),

BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly,

Type.DefaultBinder,

Type.EmptyTypes,

null);

if (createMethod != null)

{

instance = (THashAlgorithm)createMethod.Invoke(null, new object[] { });

}

else

{

instance = Activator.CreateInstance

();

}

return instance;

}

}

public static class HashAlgorithmHelper

{

static readonly char[] Digitals = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

static string ToString(byte[] bytes)

{

unsafe

{

const int byte_len = 2; // 表示一个 byte 的字符长度。

var str = new string('\0', byte_len * bytes.Length);

fixed(char* pStr = str)

{

var pStr2 = pStr; // fixed pStr 是只读的,所以我们定义一个变量。

foreach (var item in bytes)

{

*pStr2 = Digitals[item >> 4/* byte high */]; ++pStr2;

*pStr2 = Digitals[item & 15/* byte high */]; ++pStr2;

}

}

return str;

}

}

public static string ComputeHash

(string input) where THashAlgorithm : HashAlgorithm

{

var bytes = Encoding.UTF8.GetBytes(input);

return ToString(THashAlgorithmInstances

.Instance.ComputeHash(bytes));

}

}

class Program

{

public static string GetMd5Hash(string input)

{

using (MD5 md5Hash = MD5.Create())

{

// Convert the input string to a byte array and compute the hash.

byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes

// and create a string.

StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data

// and format each one as a hexadecimal string.

for (int i = 0; i

{

sBuilder.Append(data[i].ToString("x2"));

}

// Return the hexadecimal string.

return sBuilder.ToString();

}

}

static void Main(string[] args)

{

Console.WriteLine(HashAlgorithmHelper.ComputeHash

("Hello World!"));

Console.WriteLine(GetMd5Hash("Hello World!"));

while (true)

{

var stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

HashAlgorithmHelper.ComputeHash

("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

GetMd5Hash("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

}

}

}

不包含不安全代码的通用版本:

using System;

using System.Diagnostics;

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Security.Cryptography;

using System.Text;

static class THashAlgorithmInstances

where THashAlgorithm : HashAlgorithm

{

///

/// 线程静态变量。

/// 即:这个变量在每个线程中都是唯一的。

/// 再结合泛型类实现了该变量在不同泛型或不同的线程先的变量都是唯一的。

/// 这样做的目的是为了避开多线程问题。

///

[ThreadStatic]

static THashAlgorithm instance;

public static THashAlgorithm Instance => instance ?? Create();

///

/// 寻找 THashAlgorithm 类型下的 Create 静态方法,并执行它。

/// 如果没找到,则执行 Activator.CreateInstance 调用构造方法创建实例。

/// 如果 Activator.CreateInstance 方法执行失败,它会抛出异常。

///

[MethodImpl(MethodImplOptions.NoInlining)]

static THashAlgorithm Create()

{

var createMethod = typeof(THashAlgorithm).GetMethod(

nameof(HashAlgorithm.Create),

BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly,

Type.DefaultBinder,

Type.EmptyTypes,

null);

if (createMethod != null)

{

instance = (THashAlgorithm)createMethod.Invoke(null, new object[] { });

}

else

{

instance = Activator.CreateInstance

();

}

return instance;

}

}

public static class HashAlgorithmHelper

{

static readonly char[] Digitals = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

static string ToString(byte[] bytes)

{

const int byte_len = 2; // 表示一个 byte 的字符长度。

var chars = new char[byte_len * bytes.Length];

var index = 0;

foreach (var item in bytes)

{

chars[index] = Digitals[item >> 4/* byte high */]; ++index;

chars[index] = Digitals[item & 15/* byte high */]; ++index;

}

return new string(chars);

}

public static string ComputeHash

(string input) where THashAlgorithm : HashAlgorithm

{

var bytes = Encoding.UTF8.GetBytes(input);

return ToString(THashAlgorithmInstances

.Instance.ComputeHash(bytes));

}

}

class Program

{

public static string GetMd5Hash(string input)

{

using (MD5 md5Hash = MD5.Create())

{

// Convert the input string to a byte array and compute the hash.

byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes

// and create a string.

StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data

// and format each one as a hexadecimal string.

for (int i = 0; i

{

sBuilder.Append(data[i].ToString("x2"));

}

// Return the hexadecimal string.

return sBuilder.ToString();

}

}

static void Main(string[] args)

{

Console.WriteLine(HashAlgorithmHelper.ComputeHash

("Hello World!"));

Console.WriteLine(GetMd5Hash("Hello World!"));

while (true)

{

var stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

HashAlgorithmHelper.ComputeHash

("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

stopwatch = Stopwatch.StartNew();

for (int i = 0; i

{

GetMd5Hash("Hello World!");

}

Console.WriteLine(stopwatch.ElapsedMilliseconds);

}

}

}

不包含不安全代码通用版本的性能:(性能依然极佳,建议使用此版本)

转载自:.Net技术

力软.Net敏捷开发框架

财经自媒体联盟更多自媒体作者

新浪首页 语音播报 相关新闻 返回顶部