Skip to content

Commit

Permalink
Merge pull request #131 from jamesmontemagno/fix130
Browse files Browse the repository at this point in the history
Use new thread save MD5 as long as no hashAlgorithm was specified.
  • Loading branch information
jamesmontemagno authored Apr 25, 2023
2 parents c2f0036 + f184420 commit cb9be41
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 cb9be41

Please sign in to comment.