diff --git a/src/AgileConfig.Server.Common/Encrypt.cs b/src/AgileConfig.Server.Common/Encrypt.cs index 6690345f..f4e68448 100644 --- a/src/AgileConfig.Server.Common/Encrypt.cs +++ b/src/AgileConfig.Server.Common/Encrypt.cs @@ -1,16 +1,17 @@ using System; using System.Security.Cryptography; using System.Text; +using System.Threading; namespace AgileConfig.Server.Common { public static class Encrypt { - private static readonly MD5 Md5Instance = MD5.Create(); + private static readonly ThreadLocal Md5Instance = new ThreadLocal(MD5.Create); public static string Md5(string txt) { var inputBytes = Encoding.ASCII.GetBytes(txt); - var hashBytes = Md5Instance.ComputeHash(inputBytes); + var hashBytes = Md5Instance.Value.ComputeHash(inputBytes); return Convert.ToHexString(hashBytes); } } diff --git a/test/AgileConfig.Server.CommonTests/EncryptTests.cs b/test/AgileConfig.Server.CommonTests/EncryptTests.cs new file mode 100644 index 00000000..4660cf5a --- /dev/null +++ b/test/AgileConfig.Server.CommonTests/EncryptTests.cs @@ -0,0 +1,28 @@ +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AgileConfig.Server.Common.Tests; + +[TestClass] +public class EncryptTests +{ + private const string Origin = "123456"; + private const string Expected = "E10ADC3949BA59ABBE56E057F20F883E"; + + [TestMethod] + public void Md5Test() + { + var result = Encrypt.Md5(Origin); + Assert.AreEqual(Expected, result); + } + + [TestMethod] + public void ParallelCallTest() + { + Parallel.For(0, 1000, _ => + { + var result = Encrypt.Md5(Origin); + Assert.AreEqual(Expected, result); + }); + } +} \ No newline at end of file