From 6325fd4c16bde8807062b3de978e297a9cf4c52f Mon Sep 17 00:00:00 2001 From: MartinYl <83362632@qq.com> Date: Fri, 13 Nov 2020 20:30:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8E=E5=8E=BB=E9=99=A4unsafe=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E4=BC=98=E5=8C=96MD5=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E6=80=A7=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96MD5=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/Fur/DataEncryption/MD5Encryption.cs | 64 +++++++++++++------ framework/Fur/Fur.csproj | 1 - 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/framework/Fur/DataEncryption/MD5Encryption.cs b/framework/Fur/DataEncryption/MD5Encryption.cs index 58d2200067..45bc96f0f6 100644 --- a/framework/Fur/DataEncryption/MD5Encryption.cs +++ b/framework/Fur/DataEncryption/MD5Encryption.cs @@ -1,6 +1,6 @@ using Fur.DependencyInjection; using System; -using System.Reflection; +using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text; @@ -10,16 +10,9 @@ namespace Fur.DataEncryption /// MD5 加密 /// [SkipScan] - public unsafe class MD5Encryption + public class MD5Encryption { - private const uint LOWERCASING = 0x2020U; - - private static readonly delegate* managed, Span, uint, void> _EncodeToUtf16Ptr; - - static MD5Encryption() - { - _EncodeToUtf16Ptr = (delegate* managed, Span, uint, void>)typeof(uint).Assembly.GetType("System.HexConverter").GetMethod("EncodeToUtf16", BindingFlags.Static | BindingFlags.Public).MethodHandle.GetFunctionPointer(); - } + private static readonly char[] Digitals = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /// /// 字符串 MD5 比较 @@ -29,10 +22,8 @@ namespace Fur.DataEncryption /// bool public static bool Compare(string text, string hash) { - using var md5Hash = MD5.Create(); var hashOfInput = Encrypt(text); - var comparer = StringComparer.OrdinalIgnoreCase; - return 0 == comparer.Compare(hashOfInput, hash); + return hash.Equals(hashOfInput, StringComparison.OrdinalIgnoreCase); } /// @@ -42,11 +33,48 @@ namespace Fur.DataEncryption /// public static string Encrypt(string text) { - using var md5Hash = MD5.Create(); - var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(text)); - Span output = stackalloc char[32]; - _EncodeToUtf16Ptr(data, output, LOWERCASING); - return new string(output); + var bytes = Encoding.UTF8.GetBytes(text); + + return ByteToString(MD5Instances.Instance.ComputeHash(bytes)); + } + /// + /// 创建MD5实例 + /// + private static class MD5Instances + { + /// + /// 线程静态变量 + /// + [ThreadStatic] + private static MD5 instance; + /// + /// MD5实例 + /// + public static MD5 Instance => instance ?? Create(); + + [MethodImpl(MethodImplOptions.NoInlining)] + private static MD5 Create() + { + instance = MD5.Create() ?? Activator.CreateInstance(); + + return instance; + } + } + /// + /// 重写ToString方法 + /// + /// + /// + private static string ByteToString(byte[] bytes) + { + var chars = new char[bytes.Length * 2]; + var index = 0; + foreach (var item in bytes) + { + chars[index] = Digitals[item >> 4]; ++index; + chars[index] = Digitals[item & 15]; ++index; + } + return new string(chars, 0, chars.Length); } } } \ No newline at end of file diff --git a/framework/Fur/Fur.csproj b/framework/Fur/Fur.csproj index d4eb98628b..4d8d46baf4 100644 --- a/framework/Fur/Fur.csproj +++ b/framework/Fur/Fur.csproj @@ -2,7 +2,6 @@ net5.0 - true 百小僧/MonkSoul Baiqian Co.,Ltd. © 2020 Fur, Baiqian Co.,Ltd. -- Gitee