66namespace Swan . Cryptography
77{
88 /// <summary>
9- /// Use this class to compute a hash in MD4 , SHA1, SHA256 or SHA512.
9+ /// Use this class to compute a hash in MD5 , SHA1, SHA256 or SHA512.
1010 /// </summary>
1111 public static class Hasher
1212 {
13- private static readonly Lazy < MD5 > Md5Hasher = new ( MD5 . Create , true ) ;
14- private static readonly Lazy < SHA1 > SHA1Hasher = new ( SHA1 . Create , true ) ;
15- private static readonly Lazy < SHA256 > SHA256Hasher = new ( SHA256 . Create , true ) ;
16- private static readonly Lazy < SHA512 > SHA512Hasher = new ( SHA512 . Create , true ) ;
17-
1813 /// <summary>
1914 /// Computes the MD5 hash of the given stream.
2015 /// Do not use for large streams as this reads ALL bytes at once.
2116 /// </summary>
2217 /// <param name="this">The stream.</param>
23- /// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
2418 /// <returns>
2519 /// The computed hash code.
2620 /// </returns>
2721 /// <exception cref="ArgumentNullException">stream.</exception>
2822 [ Obsolete ( "Use a better hasher." ) ]
29- public static byte [ ] ComputeMD5 ( Stream @this , bool createHasher = false )
23+ public static byte [ ] ComputeMD5 ( this Stream @this )
3024 {
3125 if ( @this == null )
3226 throw new ArgumentNullException ( nameof ( @this ) ) ;
@@ -52,7 +46,7 @@ public static byte[] ComputeMD5(Stream @this, bool createHasher = false)
5246 }
5347 while ( readAheadBytesRead != 0 ) ;
5448
55- return md5 . Hash ;
49+ return md5 . Hash ?? Array . Empty < byte > ( ) ;
5650 }
5751
5852 /// <summary>
@@ -72,8 +66,17 @@ public static byte[] ComputeMD5(string value, bool createHasher = false) =>
7266 /// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
7367 /// <returns>The computed hash code.</returns>
7468 [ Obsolete ( "Use a better hasher." ) ]
75- public static byte [ ] ComputeMD5 ( byte [ ] data , bool createHasher = false ) =>
76- ( createHasher ? MD5 . Create ( ) : Md5Hasher . Value ) . ComputeHash ( data ) ;
69+ public static byte [ ] ComputeMD5 ( byte [ ] data , bool createHasher = false )
70+ {
71+ if ( createHasher )
72+ {
73+ using var hasher = MD5 . Create ( ) ;
74+ hasher . ComputeHash ( data ) ;
75+ }
76+
77+ return MD5 . HashData ( data ) ;
78+ }
79+
7780
7881 /// <summary>
7982 /// Computes the SHA-1 hash of the given string using UTF8 byte encoding.
@@ -88,7 +91,14 @@ public static byte[] ComputeMD5(byte[] data, bool createHasher = false) =>
8891 public static byte [ ] ComputeSha1 ( string @this , bool createHasher = false )
8992 {
9093 var inputBytes = Encoding . UTF8 . GetBytes ( @this ) ;
91- return ( createHasher ? SHA1 . Create ( ) : SHA1Hasher . Value ) . ComputeHash ( inputBytes ) ;
94+
95+ if ( createHasher )
96+ {
97+ using var hasher = SHA1 . Create ( ) ;
98+ return hasher . ComputeHash ( inputBytes ) ;
99+ }
100+
101+ return SHA1 . HashData ( inputBytes ) ;
92102 }
93103
94104 /// <summary>
@@ -103,7 +113,13 @@ public static byte[] ComputeSha1(string @this, bool createHasher = false)
103113 public static byte [ ] ComputeSha256 ( string value , bool createHasher = false )
104114 {
105115 var inputBytes = Encoding . UTF8 . GetBytes ( value ) ;
106- return ( createHasher ? SHA256 . Create ( ) : SHA256Hasher . Value ) . ComputeHash ( inputBytes ) ;
116+ if ( createHasher )
117+ {
118+ using var hasher = SHA256 . Create ( ) ;
119+ return hasher . ComputeHash ( inputBytes ) ;
120+ }
121+
122+ return SHA256 . HashData ( inputBytes ) ;
107123 }
108124
109125 /// <summary>
@@ -118,7 +134,14 @@ public static byte[] ComputeSha256(string value, bool createHasher = false)
118134 public static byte [ ] ComputeSha512 ( string value , bool createHasher = false )
119135 {
120136 var inputBytes = Encoding . UTF8 . GetBytes ( value ) ;
121- return ( createHasher ? SHA512 . Create ( ) : SHA512Hasher . Value ) . ComputeHash ( inputBytes ) ;
137+
138+ if ( createHasher )
139+ {
140+ using var hasher = SHA512 . Create ( ) ;
141+ return hasher . ComputeHash ( inputBytes ) ;
142+ }
143+
144+ return SHA512 . HashData ( inputBytes ) ;
122145 }
123146 }
124147}
0 commit comments