Skip to content

Commit d31e4b1

Browse files
Merge pull request #471 from johelvisguzman/utility
Moved the internal string hashing extensions to a Hasher utility class
2 parents 4c30f82 + f0ecb68 commit d31e4b1

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

src/DotNetToolkit.Repository/Extensions/CachingProviderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ private static string FormatHashedKey<T>([NotNull] this ICacheProvider cacheProv
552552
Glue,
553553
CacheProviderManager.GlobalCachingPrefixCounter,
554554
cacheProvider.GetCachingPrefixCounter<T>(),
555-
key.ToMD5(),
555+
Hasher.ComputeMD5(key),
556556
CacheProviderManager.CachePrefix);
557557
}
558558

src/DotNetToolkit.Repository/Extensions/StringExtensions.cs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,6 @@ public static string Indent([CanBeNull] this string value, int size)
2626
return sb.ToString().Substring(1);
2727
}
2828

29-
public static string ToSHA256([NotNull] this string value)
30-
{
31-
Guard.NotEmpty(value, nameof(value));
32-
33-
var sha256 = System.Security.Cryptography.SHA256.Create();
34-
var hashInBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(value));
35-
var sb = new StringBuilder();
36-
37-
foreach (var b in hashInBytes)
38-
{
39-
sb.Append(b.ToString("x2"));
40-
}
41-
42-
return sb.ToString();
43-
}
44-
45-
public static string ToMD5([NotNull] this string value)
46-
{
47-
Guard.NotEmpty(value, nameof(value));
48-
49-
var bytes = Encoding.Unicode.GetBytes(value.ToCharArray());
50-
51-
using (var md5 = System.Security.Cryptography.MD5.Create())
52-
{
53-
var hash = md5.ComputeHash(bytes);
54-
55-
return hash
56-
.Aggregate(
57-
new StringBuilder(32),
58-
(sb, b) => sb.Append(b.ToString("x2")))
59-
.ToString();
60-
}
61-
}
62-
6329
public static bool ToBoolean(this string value)
6430
{
6531
if (value == null || value.Trim().Length == 0)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace DotNetToolkit.Repository.Utility
2+
{
3+
using JetBrains.Annotations;
4+
using System.Linq;
5+
using System.Security.Cryptography;
6+
using System.Text;
7+
8+
/// <summary>
9+
/// Contains various utilities for hashing.
10+
/// </summary>
11+
internal static class Hasher
12+
{
13+
public static string ComputeMD5([NotNull] string value)
14+
{
15+
using (var md5 = MD5.Create())
16+
{
17+
return ComputeHashAlgorithm(md5, value);
18+
}
19+
}
20+
21+
private static string ComputeHashAlgorithm([NotNull] HashAlgorithm algorithm, [NotNull] string value)
22+
{
23+
Guard.NotNull(algorithm, nameof(algorithm));
24+
Guard.NotEmpty(value, nameof(value));
25+
26+
var bytes = Encoding.Unicode.GetBytes(value.ToCharArray());
27+
var hash = algorithm.ComputeHash(bytes);
28+
29+
return hash
30+
.Aggregate(
31+
new StringBuilder(32),
32+
(sb, b) => sb.Append(b.ToString("x2")))
33+
.ToString();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)