From 96b1ddefc5af49538d703933cd483f93e94870e2 Mon Sep 17 00:00:00 2001
From: Executor <847529602@qq.com>
Date: Wed, 11 Nov 2020 08:38:00 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Fur=E4=B8=8BDataEncryption?=
=?UTF-8?q?=E6=80=A7=E8=83=BD=20AESEncryption.Encrypt:=20=E9=81=BF?=
=?UTF-8?q?=E5=85=8D=E9=87=8D=E6=96=B0=E5=88=86=E9=85=8DdecryptedContent?=
=?UTF-8?q?=E5=92=8Cresult=E3=80=82=E4=BD=BF=E7=94=A8Unsafe=E9=81=BF?=
=?UTF-8?q?=E5=85=8D=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E7=B1=BB=E5=9E=8B?=
=?UTF-8?q?=E8=BD=AC=E6=8D=A2=E3=80=82=20AESEncryption.Decrypt:=20?=
=?UTF-8?q?=E4=BD=BF=E7=94=A8Unsafe=E9=81=BF=E5=85=8D=E4=B8=8D=E5=BF=85?=
=?UTF-8?q?=E8=A6=81=E7=9A=84=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E3=80=82?=
=?UTF-8?q?=20MD5Encryption.Compare:=20=E5=88=A0=E9=99=A4=E5=86=97?=
=?UTF-8?q?=E4=BD=99=E4=BB=A3=E7=A0=81=E3=80=82=20MD5Encryption.Encrypt:?=
=?UTF-8?q?=20=E4=BD=BF=E7=94=A8=E6=8C=87=E5=90=91BCL=E7=9A=84=E5=87=BD?=
=?UTF-8?q?=E6=95=B0=E6=8C=87=E9=92=88=E5=92=8C=E6=A0=88=E4=B8=8A=E5=86=85?=
=?UTF-8?q?=E5=AD=98=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
framework/Fur/DataEncryption/AESEncryption.cs | 38 +++++++++----------
framework/Fur/DataEncryption/MD5Encryption.cs | 25 +++++++-----
framework/Fur/Fur.csproj | 1 +
3 files changed, 35 insertions(+), 29 deletions(-)
diff --git a/framework/Fur/DataEncryption/AESEncryption.cs b/framework/Fur/DataEncryption/AESEncryption.cs
index 8187e15588..1101b2548c 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 e8e4c303a1..58d2200067 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 7f9454e706..b943496ff2 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.
--
Gitee