Skip to content

Commit

Permalink
Use new thread save MD5 as long as no hashAlgorithm was specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Apr 21, 2023
1 parent 4dae380 commit 948fc31
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/MonkeyCache.FileStore/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Barrel : IBarrel
ReaderWriterLockSlim indexLocker;
Lazy<string> baseDirectory;
HashAlgorithm hashAlgorithm;
object locker = new object();

/// <summary>
/// FileStore Barrel constructor
Expand All @@ -32,8 +33,6 @@ public class Barrel : IBarrel
});

hashAlgorithm = hash;
if (hashAlgorithm == null)
hashAlgorithm = MD5.Create();

indexLocker = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 948fc31

Please sign in to comment.