44using System ;
55using System . Collections . Generic ;
66using System . Collections . ObjectModel ;
7+ using System . Diagnostics ;
78using System . IO ;
89using System . Management . Automation ;
910using System . Security . Cryptography ;
@@ -68,15 +69,6 @@ public string[] LiteralPath
6869 [ Parameter ( Mandatory = true , ParameterSetName = StreamParameterSet , Position = 0 ) ]
6970 public Stream InputStream { get ; set ; }
7071
71- /// <summary>
72- /// BeginProcessing() override.
73- /// This is for hash function init.
74- /// </summary>
75- protected override void BeginProcessing ( )
76- {
77- InitHasher ( Algorithm ) ;
78- }
79-
8072 /// <summary>
8173 /// ProcessRecord() override.
8274 /// This is for paths collecting from pipe.
@@ -133,6 +125,26 @@ protected override void ProcessRecord()
133125 }
134126 }
135127
128+ private byte [ ] ComputeHash ( Stream stream )
129+ {
130+ switch ( Algorithm )
131+ {
132+ case HashAlgorithmNames . SHA1 :
133+ return SHA1 . HashData ( stream ) ;
134+ case HashAlgorithmNames . SHA256 :
135+ return SHA256 . HashData ( stream ) ;
136+ case HashAlgorithmNames . SHA384 :
137+ return SHA384 . HashData ( stream ) ;
138+ case HashAlgorithmNames . SHA512 :
139+ return SHA512 . HashData ( stream ) ;
140+ case HashAlgorithmNames . MD5 :
141+ return MD5 . HashData ( stream ) ;
142+ }
143+
144+ Debug . Assert ( false , "invalid hash algorithm" ) ;
145+ return SHA256 . HashData ( stream ) ;
146+ }
147+
136148 /// <summary>
137149 /// Perform common error checks.
138150 /// Populate source code.
@@ -141,12 +153,9 @@ protected override void EndProcessing()
141153 {
142154 if ( ParameterSetName == StreamParameterSet )
143155 {
144- byte [ ] bytehash = null ;
145- string hash = null ;
156+ byte [ ] bytehash = ComputeHash ( InputStream ) ;
146157
147- bytehash = hasher . ComputeHash ( InputStream ) ;
148-
149- hash = BitConverter . ToString ( bytehash ) . Replace ( "-" , string . Empty ) ;
158+ string hash = BitConverter . ToString ( bytehash ) . Replace ( "-" , string . Empty ) ;
150159 WriteHashResult ( Algorithm , hash , string . Empty ) ;
151160 }
152161 }
@@ -159,16 +168,15 @@ protected override void EndProcessing()
159168 /// <returns>Boolean value indicating whether the hash calculation succeeded or failed.</returns>
160169 private bool ComputeFileHash ( string path , out string hash )
161170 {
162- byte [ ] bytehash = null ;
163171 Stream openfilestream = null ;
164172
165173 hash = null ;
166174
167175 try
168176 {
169177 openfilestream = File . OpenRead ( path ) ;
178+ byte [ ] bytehash = ComputeHash ( openfilestream ) ;
170179
171- bytehash = hasher . ComputeHash ( openfilestream ) ;
172180 hash = BitConverter . ToString ( bytehash ) . Replace ( "-" , string . Empty ) ;
173181 }
174182 catch ( FileNotFoundException ex )
@@ -259,11 +267,6 @@ public string Algorithm
259267
260268 private string _Algorithm = HashAlgorithmNames . SHA256 ;
261269
262- /// <summary>
263- /// Hash algorithm is used.
264- /// </summary>
265- protected HashAlgorithm hasher ;
266-
267270 /// <summary>
268271 /// Hash algorithm names.
269272 /// </summary>
@@ -275,40 +278,6 @@ internal static class HashAlgorithmNames
275278 public const string SHA384 = "SHA384" ;
276279 public const string SHA512 = "SHA512" ;
277280 }
278-
279- /// <summary>
280- /// Init a hash algorithm.
281- /// </summary>
282- protected void InitHasher ( string Algorithm )
283- {
284- try
285- {
286- switch ( Algorithm )
287- {
288- case HashAlgorithmNames . SHA1 :
289- hasher = SHA1 . Create ( ) ;
290- break ;
291- case HashAlgorithmNames . SHA256 :
292- hasher = SHA256 . Create ( ) ;
293- break ;
294- case HashAlgorithmNames . SHA384 :
295- hasher = SHA384 . Create ( ) ;
296- break ;
297- case HashAlgorithmNames . SHA512 :
298- hasher = SHA512 . Create ( ) ;
299- break ;
300- case HashAlgorithmNames . MD5 :
301- hasher = MD5 . Create ( ) ;
302- break ;
303- }
304- }
305- catch
306- {
307- // Seems it will never throw! Remove?
308- Exception exc = new NotSupportedException ( UtilityCommonStrings . AlgorithmTypeNotSupported ) ;
309- ThrowTerminatingError ( new ErrorRecord ( exc , "AlgorithmTypeNotSupported" , ErrorCategory . NotImplemented , null ) ) ;
310- }
311- }
312281 }
313282
314283 /// <summary>
0 commit comments