Skip to content

Commit cba9dac

Browse files
authored
Rewrite Get-FileHash to use static HashData methods (PowerShell#18471)
1 parent 20f0b6e commit cba9dac

File tree

3 files changed

+24
-63
lines changed

3 files changed

+24
-63
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Management.Automation;
910
using 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>

src/Microsoft.PowerShell.Commands.Utility/commands/utility/UtilityCommon.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ public static class UtilityResources
8282
/// </summary>
8383
public static string FormatHexPathPrefix { get { return UtilityCommonStrings.FormatHexPathPrefix; } }
8484

85-
/// <summary>
86-
/// Error message to indicate that requested algorithm is not supported on the target platform.
87-
/// </summary>
88-
public static string AlgorithmTypeNotSupported { get { return UtilityCommonStrings.AlgorithmTypeNotSupported; } }
89-
9085
/// <summary>
9186
/// The file '{0}' could not be parsed as a PowerShell Data File.
9287
/// </summary>

src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@
162162
<data name="PSPrefixReservedInInformationTag" xml:space="preserve">
163163
<value>Cannot use tag '{0}'. The 'PS' prefix is reserved.</value>
164164
</data>
165-
<data name="AlgorithmTypeNotSupported" xml:space="preserve">
166-
<value>Algorithm '{0}' is not supported in this system.</value>
167-
</data>
168165
<data name="CouldNotParseAsPowerShellDataFile" xml:space="preserve">
169166
<value>The file '{0}' could not be parsed as a PowerShell Data File.</value>
170167
</data>

0 commit comments

Comments
 (0)