diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs index 40ea1d187..bf80ccbff 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs @@ -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; } - /// - /// BeginProcessing() override. - /// This is for hash function init. - /// - protected override void BeginProcessing() - { - InitHasher(Algorithm); - } - /// /// 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); + } + /// /// 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 /// Boolean value indicating whether the hash calculation succeeded or failed. 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; - /// - /// Hash algorithm is used. - /// - protected HashAlgorithm hasher; - /// /// Hash algorithm names. /// @@ -275,40 +278,6 @@ namespace Microsoft.PowerShell.Commands public const string SHA384 = "SHA384"; public const string SHA512 = "SHA512"; } - - /// - /// Init a hash algorithm. - /// - 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)); - } - } } /// diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UtilityCommon.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UtilityCommon.cs index 3906405d9..327e6f19c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UtilityCommon.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UtilityCommon.cs @@ -82,11 +82,6 @@ namespace Microsoft.PowerShell.Commands /// public static string FormatHexPathPrefix { get { return UtilityCommonStrings.FormatHexPathPrefix; } } - /// - /// Error message to indicate that requested algorithm is not supported on the target platform. - /// - public static string AlgorithmTypeNotSupported { get { return UtilityCommonStrings.AlgorithmTypeNotSupported; } } - /// /// The file '{0}' could not be parsed as a PowerShell Data File. /// diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx index f75c74920..b3abafdd0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx @@ -162,9 +162,6 @@ Cannot use tag '{0}'. The 'PS' prefix is reserved. - - Algorithm '{0}' is not supported in this system. - The file '{0}' could not be parsed as a PowerShell Data File.