@@ -7,23 +7,14 @@ namespace ModularPipelines.Context;
77/// Provides hashing operations using various algorithms.
88/// </summary>
99/// <remarks>
10- /// HashAlgorithm instances are not thread-safe, so we use ThreadLocal to cache
11- /// one instance per thread. This avoids the allocation overhead of creating new
12- /// instances on every call while maintaining thread safety.
10+ /// Uses the static HashData methods available in .NET 5+ which are thread-safe
11+ /// and don't require disposal, avoiding resource leaks.
1312/// </remarks>
1413internal class Hasher : IHasher
1514{
1615 private readonly IHex _hex ;
1716 private readonly IBase64 _base64 ;
1817
19- // ThreadLocal instances for thread-safe caching of hash algorithms
20- // HashAlgorithm is not thread-safe, so each thread gets its own instance
21- private static readonly ThreadLocal < SHA1 > Sha1Algorithm = new ( ( ) => SHA1 . Create ( ) ) ;
22- private static readonly ThreadLocal < SHA256 > Sha256Algorithm = new ( ( ) => SHA256 . Create ( ) ) ;
23- private static readonly ThreadLocal < SHA384 > Sha384Algorithm = new ( ( ) => SHA384 . Create ( ) ) ;
24- private static readonly ThreadLocal < SHA512 > Sha512Algorithm = new ( ( ) => SHA512 . Create ( ) ) ;
25- private static readonly ThreadLocal < MD5 > Md5Algorithm = new ( ( ) => MD5 . Create ( ) ) ;
26-
2718 public Hasher ( IHex hex , IBase64 base64 )
2819 {
2920 _hex = hex ;
@@ -32,33 +23,31 @@ public Hasher(IHex hex, IBase64 base64)
3223
3324 public string Sha1 ( string input , HashType hashType = HashType . Hex )
3425 {
35- return ComputeHash ( Sha1Algorithm . Value ! , input , hashType ) ;
26+ var bytes = System . Security . Cryptography . SHA1 . HashData ( Encoding . UTF8 . GetBytes ( input ) ) ;
27+ return hashType == HashType . Hex ? _hex . ToHex ( bytes ) : _base64 . ToBase64String ( bytes ) ;
3628 }
3729
3830 public string Sha256 ( string input , HashType hashType = HashType . Hex )
3931 {
40- return ComputeHash ( Sha256Algorithm . Value ! , input , hashType ) ;
32+ var bytes = System . Security . Cryptography . SHA256 . HashData ( Encoding . UTF8 . GetBytes ( input ) ) ;
33+ return hashType == HashType . Hex ? _hex . ToHex ( bytes ) : _base64 . ToBase64String ( bytes ) ;
4134 }
4235
4336 public string Sha384 ( string input , HashType hashType = HashType . Hex )
4437 {
45- return ComputeHash ( Sha384Algorithm . Value ! , input , hashType ) ;
38+ var bytes = System . Security . Cryptography . SHA384 . HashData ( Encoding . UTF8 . GetBytes ( input ) ) ;
39+ return hashType == HashType . Hex ? _hex . ToHex ( bytes ) : _base64 . ToBase64String ( bytes ) ;
4640 }
4741
4842 public string Sha512 ( string input , HashType hashType = HashType . Hex )
4943 {
50- return ComputeHash ( Sha512Algorithm . Value ! , input , hashType ) ;
44+ var bytes = System . Security . Cryptography . SHA512 . HashData ( Encoding . UTF8 . GetBytes ( input ) ) ;
45+ return hashType == HashType . Hex ? _hex . ToHex ( bytes ) : _base64 . ToBase64String ( bytes ) ;
5146 }
5247
5348 public string Md5 ( string input , HashType hashType = HashType . Hex )
5449 {
55- return ComputeHash ( Md5Algorithm . Value ! , input , hashType ) ;
56- }
57-
58- private string ComputeHash ( HashAlgorithm hashAlgorithm , string input , HashType hashType )
59- {
60- var bytes = hashAlgorithm . ComputeHash ( Encoding . UTF8 . GetBytes ( input ) ) ;
61-
50+ var bytes = System . Security . Cryptography . MD5 . HashData ( Encoding . UTF8 . GetBytes ( input ) ) ;
6251 return hashType == HashType . Hex ? _hex . ToHex ( bytes ) : _base64 . ToBase64String ( bytes ) ;
6352 }
6453}
0 commit comments