6
6
namespace Swan . Cryptography
7
7
{
8
8
/// <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.
10
10
/// </summary>
11
11
public static class Hasher
12
12
{
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
-
18
13
/// <summary>
19
14
/// Computes the MD5 hash of the given stream.
20
15
/// Do not use for large streams as this reads ALL bytes at once.
21
16
/// </summary>
22
17
/// <param name="this">The stream.</param>
23
- /// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
24
18
/// <returns>
25
19
/// The computed hash code.
26
20
/// </returns>
27
21
/// <exception cref="ArgumentNullException">stream.</exception>
28
22
[ Obsolete ( "Use a better hasher." ) ]
29
- public static byte [ ] ComputeMD5 ( Stream @this , bool createHasher = false )
23
+ public static byte [ ] ComputeMD5 ( this Stream @this )
30
24
{
31
25
if ( @this == null )
32
26
throw new ArgumentNullException ( nameof ( @this ) ) ;
@@ -52,7 +46,7 @@ public static byte[] ComputeMD5(Stream @this, bool createHasher = false)
52
46
}
53
47
while ( readAheadBytesRead != 0 ) ;
54
48
55
- return md5 . Hash ;
49
+ return md5 . Hash ?? Array . Empty < byte > ( ) ;
56
50
}
57
51
58
52
/// <summary>
@@ -72,8 +66,17 @@ public static byte[] ComputeMD5(string value, bool createHasher = false) =>
72
66
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
73
67
/// <returns>The computed hash code.</returns>
74
68
[ 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
+
77
80
78
81
/// <summary>
79
82
/// 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) =>
88
91
public static byte [ ] ComputeSha1 ( string @this , bool createHasher = false )
89
92
{
90
93
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 ) ;
92
102
}
93
103
94
104
/// <summary>
@@ -103,7 +113,13 @@ public static byte[] ComputeSha1(string @this, bool createHasher = false)
103
113
public static byte [ ] ComputeSha256 ( string value , bool createHasher = false )
104
114
{
105
115
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 ) ;
107
123
}
108
124
109
125
/// <summary>
@@ -118,7 +134,14 @@ public static byte[] ComputeSha256(string value, bool createHasher = false)
118
134
public static byte [ ] ComputeSha512 ( string value , bool createHasher = false )
119
135
{
120
136
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 ) ;
122
145
}
123
146
}
124
147
}
0 commit comments