From 79a7528aeb8c559a7239aab54ffbc56bda0e6579 Mon Sep 17 00:00:00 2001 From: Sheldon-NULL <83170675+Sheldon-NULL@users.noreply.github.com> Date: Sat, 11 May 2024 10:49:43 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=20Md5=E5=8A=A0=E5=AF=86=20Update=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4AES=E3=80=81DES=E5=8A=A0=E5=AF=86=E7=9A=84?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4=E5=92=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AesUtil.cs | 12 ++-- .../DesUtil.cs | 6 +- EasyTool.Core/SecureCategory/MD5Utils.cs | 56 +++++++++++++++++++ 3 files changed, 62 insertions(+), 12 deletions(-) rename EasyTool.Core/{CodeCategory => SecureCategory}/AesUtil.cs (96%) rename EasyTool.Core/{CodeCategory => SecureCategory}/DesUtil.cs (95%) create mode 100644 EasyTool.Core/SecureCategory/MD5Utils.cs diff --git a/EasyTool.Core/CodeCategory/AesUtil.cs b/EasyTool.Core/SecureCategory/AesUtil.cs similarity index 96% rename from EasyTool.Core/CodeCategory/AesUtil.cs rename to EasyTool.Core/SecureCategory/AesUtil.cs index e2d2bb2..86f5737 100644 --- a/EasyTool.Core/CodeCategory/AesUtil.cs +++ b/EasyTool.Core/SecureCategory/AesUtil.cs @@ -1,14 +1,10 @@ using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Drawing; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; -using static System.Net.Mime.MediaTypeNames; -namespace EasyTool.CodeCategory +namespace EasyTool { public static class AesUtil { @@ -96,13 +92,13 @@ private static bool IsLegalSize(string sk) /// 默认UTF8 /// 加密后的结果 /// - public static string Encrypt(string text, string key, string iv ,CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) + public static string Encrypt(string text, string key, string iv, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { if (string.IsNullOrWhiteSpace(text)) return string.Empty; ; - if (!KeyIsLegalSize(key)) + if (!KeyIsLegalSize(key)) throw new ArgumentException("不合规的秘钥,请确认秘钥为16 、24、 32位的字符"); - if (!IvIsLegalSize(iv)) + if (!IvIsLegalSize(iv)) throw new ArgumentException("不合规的iv,请确认iv为16位的字符"); encoding ??= Encoding.UTF8; diff --git a/EasyTool.Core/CodeCategory/DesUtil.cs b/EasyTool.Core/SecureCategory/DesUtil.cs similarity index 95% rename from EasyTool.Core/CodeCategory/DesUtil.cs rename to EasyTool.Core/SecureCategory/DesUtil.cs index 94e350f..ac275bf 100644 --- a/EasyTool.Core/CodeCategory/DesUtil.cs +++ b/EasyTool.Core/SecureCategory/DesUtil.cs @@ -1,11 +1,9 @@ using System; -using System.Collections.Generic; -using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; -namespace EasyTool.CodeCategory +namespace EasyTool { /// /// DES工具类 @@ -78,7 +76,7 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo /// 默认UTF8 /// /// - public static string Encrypt(string str, string sk,string iv, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) + public static string Encrypt(string str, string sk, string iv, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { if (string.IsNullOrWhiteSpace(str)) return string.Empty; if (!IsLegalSize(sk)) throw new ArgumentException("不合规的秘钥,请确认秘钥为8位的字符"); diff --git a/EasyTool.Core/SecureCategory/MD5Utils.cs b/EasyTool.Core/SecureCategory/MD5Utils.cs new file mode 100644 index 0000000..0c6a8f0 --- /dev/null +++ b/EasyTool.Core/SecureCategory/MD5Utils.cs @@ -0,0 +1,56 @@ +using EasyTool.ConvertCategory; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool +{ + /// + /// MD5加密工具类 + /// + public class MD5Utils + { + /// + /// MD5加密 + /// + /// 加密字符串 + /// 是否小写 + /// + public static string Encrypt(string source, bool lowerCase = false) + { + if (string.IsNullOrWhiteSpace(source)) + return string.Empty; + + return Encrypt(Encoding.UTF8.GetBytes(source), lowerCase); + } + + /// + /// MD5加密 + /// + /// 加密字节流 + /// 是否小写 + /// + public static string Encrypt(byte[] source, bool lowerCase = false) + { + if (source == null) + return string.Empty; + + using var md5Hash = MD5.Create(); + return md5Hash.ComputeHash(source).ToHex(lowerCase); + } + + /// + /// MD5加密 + /// + /// 流 + /// 是否小写 + /// + public static string Encrypt(Stream inputStream, bool lowerCase = false) + { + if (inputStream == null) return string.Empty; + + using var md5Hash = MD5.Create(); + return md5Hash.ComputeHash(inputStream).ToHex(lowerCase); + } + } +}