Rewrite Get-FileHash to use static HashData methods (#18471)

This commit is contained in:
Berkan Diler authored and GitHub committed 2022-11-23 17:28:42 +00:00
1 parent 20f0b6e3bd
commit cba9dacb29
3 files changed
+24 -63

No files matched your search

@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using System.Security.Cryptography;
@@ -68,15 +69,6 @@ namespace Microsoft.PowerShell.Commands
[Parameter(Mandatory = true, ParameterSetName = StreamParameterSet, Position = 0)]
public Stream InputStream { get; set; }
/// <summary>
/// BeginProcessing() override.
/// This is for hash function init.
/// </summary>
protected override void BeginProcessing()
{
InitHasher(Algorithm);
}
/// <summary>
/// ProcessRecord() override.
/// This is for paths collecting from pipe.
@@ -133,6 +125,26 @@ namespace Microsoft.PowerShell.Commands
}
}
private byte[] ComputeHash(Stream stream)
{
switch (Algorithm)
{
case HashAlgorithmNames.SHA1:
return SHA1.HashData(stream);
case HashAlgorithmNames.SHA256:
return SHA256.HashData(stream);
case HashAlgorithmNames.SHA384:
return SHA384.HashData(stream);
case HashAlgorithmNames.SHA512:
return SHA512.HashData(stream);
case HashAlgorithmNames.MD5:
return MD5.HashData(stream);
}
Debug.Assert(false, "invalid hash algorithm");
return SHA256.HashData(stream);
}
/// <summary>
/// Perform common error checks.
/// Populate source code.
@@ -141,12 +153,9 @@ namespace Microsoft.PowerShell.Commands
{
if (ParameterSetName == StreamParameterSet)
{
byte[] bytehash = null;
string hash = null;
byte[] bytehash = ComputeHash(InputStream);
bytehash = hasher.ComputeHash(InputStream);
hash = BitConverter.ToString(bytehash).Replace("-", string.Empty);
string hash = BitConverter.ToString(bytehash).Replace("-", string.Empty);
WriteHashResult(Algorithm, hash, string.Empty);
}
}
@@ -159,7 +168,6 @@ namespace Microsoft.PowerShell.Commands
/// <returns>Boolean value indicating whether the hash calculation succeeded or failed.</returns>
private bool ComputeFileHash(string path, out string hash)
{
byte[] bytehash = null;
Stream openfilestream = null;
hash = null;
@@ -167,8 +175,8 @@ namespace Microsoft.PowerShell.Commands
try
{
openfilestream = File.OpenRead(path);
byte[] bytehash = ComputeHash(openfilestream);
bytehash = hasher.ComputeHash(openfilestream);
hash = BitConverter.ToString(bytehash).Replace("-", string.Empty);
}
catch (FileNotFoundException ex)
@@ -259,11 +267,6 @@ namespace Microsoft.PowerShell.Commands
private string _Algorithm = HashAlgorithmNames.SHA256;
/// <summary>
/// Hash algorithm is used.
/// </summary>
protected HashAlgorithm hasher;
/// <summary>
/// Hash algorithm names.
/// </summary>
@@ -275,40 +278,6 @@ namespace Microsoft.PowerShell.Commands
public const string SHA384 = "SHA384";
public const string SHA512 = "SHA512";
}
/// <summary>
/// Init a hash algorithm.
/// </summary>
protected void InitHasher(string Algorithm)
{
try
{
switch (Algorithm)
{
case HashAlgorithmNames.SHA1:
hasher = SHA1.Create();
break;
case HashAlgorithmNames.SHA256:
hasher = SHA256.Create();
break;
case HashAlgorithmNames.SHA384:
hasher = SHA384.Create();
break;
case HashAlgorithmNames.SHA512:
hasher = SHA512.Create();
break;
case HashAlgorithmNames.MD5:
hasher = MD5.Create();
break;
}
}
catch
{
// Seems it will never throw! Remove?
Exception exc = new NotSupportedException(UtilityCommonStrings.AlgorithmTypeNotSupported);
ThrowTerminatingError(new ErrorRecord(exc, "AlgorithmTypeNotSupported", ErrorCategory.NotImplemented, null));
}
}
}
/// <summary>
@@ -82,11 +82,6 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public static string FormatHexPathPrefix { get { return UtilityCommonStrings.FormatHexPathPrefix; } }
/// <summary>
/// Error message to indicate that requested algorithm is not supported on the target platform.
/// </summary>
public static string AlgorithmTypeNotSupported { get { return UtilityCommonStrings.AlgorithmTypeNotSupported; } }
/// <summary>
/// The file '{0}' could not be parsed as a PowerShell Data File.
/// </summary>
@@ -162,9 +162,6 @@
<data name="PSPrefixReservedInInformationTag" xml:space="preserve">
<value>Cannot use tag '{0}'. The 'PS' prefix is reserved.</value>
</data>
<data name="AlgorithmTypeNotSupported" xml:space="preserve">
<value>Algorithm '{0}' is not supported in this system.</value>
</data>
<data name="CouldNotParseAsPowerShellDataFile" xml:space="preserve">
<value>The file '{0}' could not be parsed as a PowerShell Data File.</value>
</data>