Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new thread save MD5 as long as no hashAlgorithm was specified. #131

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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