Skip to content

Commit

Permalink
Modified barrel ctor to optionally allow HashAlgorithm to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewclendening committed Oct 7, 2021
1 parent e833864 commit a0bd9cd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/MonkeyCache.FileStore/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ public class Barrel : IBarrel
ReaderWriterLockSlim indexLocker;
readonly JsonSerializerSettings jsonSettings;
Lazy<string> baseDirectory;
HashAlgorithm hashAlgorithm;

Barrel(string cacheDirectory = null)
/// <summary>
/// FileStore Barrel constructor
/// </summary>
/// <param name="cacheDirectory">Optionally specify directory where cache will live</param>
/// <param name="hash">Optionally specify hash algorithm</param>
Barrel(string cacheDirectory = null, HashAlgorithm hash = null)
{
baseDirectory = new Lazy<string>(() =>
{
Expand All @@ -24,6 +30,10 @@ public class Barrel : IBarrel
: cacheDirectory;
});

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

indexLocker = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

jsonSettings = new JsonSerializerSettings
Expand Down Expand Up @@ -456,10 +466,9 @@ void LoadIndex()
}
}

static string Hash(string input)
string Hash(string input)
{
var md5Hasher = MD5.Create();
var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
var data = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(input));
return BitConverter.ToString(data);
}

Expand Down

0 comments on commit a0bd9cd

Please sign in to comment.