From 948fc310cced4b244f0b4306d3d532146bf02ac9 Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Fri, 21 Apr 2023 09:19:19 -0700 Subject: [PATCH] Use new thread save MD5 as long as no hashAlgorithm was specified. --- src/MonkeyCache.FileStore/Barrel.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/MonkeyCache.FileStore/Barrel.cs b/src/MonkeyCache.FileStore/Barrel.cs index f65313d..f56d755 100644 --- a/src/MonkeyCache.FileStore/Barrel.cs +++ b/src/MonkeyCache.FileStore/Barrel.cs @@ -16,6 +16,7 @@ public class Barrel : IBarrel ReaderWriterLockSlim indexLocker; Lazy baseDirectory; HashAlgorithm hashAlgorithm; + object locker = new object(); /// /// FileStore Barrel constructor @@ -32,8 +33,6 @@ public class Barrel : IBarrel }); hashAlgorithm = hash; - if (hashAlgorithm == null) - hashAlgorithm = MD5.Create(); indexLocker = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); @@ -469,7 +468,18 @@ void LoadIndex() string Hash(string input) { - var data = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(input)); + byte[] data; + if(hashAlgorithm is null) + { + data = MD5.HashData(Encoding.Default.GetBytes(input)); + } + else + { + lock(locker) + { + data = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(input)); + } + } return BitConverter.ToString(data); }