diff --git a/framework/Fur/DataEncryption/AESEncryption.cs b/framework/Fur/DataEncryption/AESEncryption.cs
index 8187e155889ce01b77dd8efea276c27c885551fc..1101b2548cdc8d14f5327ee17e5185eba9d78cb3 100644
--- a/framework/Fur/DataEncryption/AESEncryption.cs
+++ b/framework/Fur/DataEncryption/AESEncryption.cs
@@ -1,6 +1,8 @@
using Fur.DependencyInjection;
using System;
+using System.Buffers.Text;
using System.IO;
+using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
@@ -25,21 +27,25 @@ namespace Fur.DataEncryption
using var aesAlg = Aes.Create();
using var encryptor = aesAlg.CreateEncryptor(encryptKey, aesAlg.IV);
using var msEncrypt = new MemoryStream();
- using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
+ using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write, true))
- using (var swEncrypt = new StreamWriter(csEncrypt))
+ using (var swEncrypt = new StreamWriter(csEncrypt, leaveOpen: true))
{
swEncrypt.Write(text);
}
var iv = aesAlg.IV;
- var decryptedContent = msEncrypt.ToArray();
- var result = new byte[iv.Length + decryptedContent.Length];
+ var dataLength = iv.Length + (int)msEncrypt.Length;
+ var decryptedContent = msEncrypt.GetBuffer();
+ var base64Length = Base64.GetMaxEncodedToUtf8Length(dataLength);
+ var result = new byte[base64Length];
- Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
- Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);
+ Unsafe.CopyBlock(ref result[0], ref iv[0], (uint)iv.Length);
+ Unsafe.CopyBlock(ref result[iv.Length], ref decryptedContent[0], (uint)msEncrypt.Length);
- return Convert.ToBase64String(result);
+ Base64.EncodeToUtf8InPlace(result, dataLength, out base64Length);
+
+ return Encoding.ASCII.GetString(result.AsSpan()[..base64Length]);
}
///
@@ -55,22 +61,16 @@ namespace Fur.DataEncryption
var iv = new byte[16];
var cipher = new byte[fullCipher.Length - iv.Length];
- Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
- Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, fullCipher.Length - iv.Length);
+ Unsafe.CopyBlock(ref iv[0], ref fullCipher[0], (uint)iv.Length);
+ Unsafe.CopyBlock(ref cipher[0], ref fullCipher[iv.Length], (uint)(fullCipher.Length - iv.Length));
var decryptKey = Encoding.UTF8.GetBytes(skey);
using var aesAlg = Aes.Create();
using var decryptor = aesAlg.CreateDecryptor(decryptKey, iv);
- string result;
- using (var msDecrypt = new MemoryStream(cipher))
- {
- using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
- using var srDecrypt = new StreamReader(csDecrypt);
-
- result = srDecrypt.ReadToEnd();
- }
-
- return result;
+ using var msDecrypt = new MemoryStream(cipher);
+ using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
+ using var srDecrypt = new StreamReader(csDecrypt);
+ return srDecrypt.ReadToEnd();
}
}
}
\ No newline at end of file
diff --git a/framework/Fur/DataEncryption/MD5Encryption.cs b/framework/Fur/DataEncryption/MD5Encryption.cs
index e8e4c303a1d8824822d77e55971f9eeb133b0e8d..58d22000679ee32c1b5c50ce7ef7ee03791ec5c0 100644
--- a/framework/Fur/DataEncryption/MD5Encryption.cs
+++ b/framework/Fur/DataEncryption/MD5Encryption.cs
@@ -1,5 +1,6 @@
using Fur.DependencyInjection;
using System;
+using System.Reflection;
using System.Security.Cryptography;
using System.Text;
@@ -9,8 +10,17 @@ namespace Fur.DataEncryption
/// MD5 加密
///
[SkipScan]
- public class MD5Encryption
+ public unsafe 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();
+ }
+
///
/// 字符串 MD5 比较
///
@@ -22,8 +32,7 @@ namespace Fur.DataEncryption
using var md5Hash = MD5.Create();
var hashOfInput = Encrypt(text);
var comparer = StringComparer.OrdinalIgnoreCase;
- if (0 == comparer.Compare(hashOfInput, hash)) return true;
- else return false;
+ return 0 == comparer.Compare(hashOfInput, hash);
}
///
@@ -35,13 +44,9 @@ namespace Fur.DataEncryption
{
using var md5Hash = MD5.Create();
var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(text));
-
- var sBuilder = new StringBuilder();
- for (var i = 0; i < data.Length; i++)
- {
- sBuilder.Append(data[i].ToString("x2"));
- }
- return sBuilder.ToString();
+ Span output = stackalloc char[32];
+ _EncodeToUtf16Ptr(data, output, LOWERCASING);
+ return new string(output);
}
}
}
\ No newline at end of file
diff --git a/framework/Fur/Fur.csproj b/framework/Fur/Fur.csproj
index 7f9454e7061bfebb429f7872dec5012e063c20cf..b943496ff2e781dd3683cde7eebd4d9aff7e12ca 100644
--- a/framework/Fur/Fur.csproj
+++ b/framework/Fur/Fur.csproj
@@ -2,6 +2,7 @@
net5.0
+ true
百小僧/MonkSoul
Baiqian Co.,Ltd.
© 2020 Fur, Baiqian Co.,Ltd.