Skip to content

Commit 371a6a2

Browse files
authored
Merge pull request #1 from bam-t/v1.0.2
fixed issues caused by a space in file path
2 parents c9cc09d + 844abc3 commit 371a6a2

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

HashTools/MainWindow.xaml.cs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private async void btn_GenerateHash_Click(object sender, RoutedEventArgs e)
208208
// display generated file hash details
209209
txtblk_run_algorithmType.Text = algorithm;
210210
txtblk_run_fileInfo.Text = $" hash of {filePath}";
211-
txtblk_hashResult.Text = hash;
211+
txtblk_hashResult.Text = hash.ToUpper();
212212
txtblk_hashResult.Foreground = Brushes.Green;
213213
btn_copyFileHash.Visibility = Visibility.Visible;
214214
}
@@ -624,9 +624,7 @@ bool removeAppFromStartMenu()
624624
/// <returns>Returns the calculated file hash. An empty string will be returned if calculation failed.</returns>
625625
Task<string> generateHashAsync(string filePath, string algorithm)
626626
{
627-
// use a task completion source to run IO bound processes
628-
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
629-
try
627+
return Task.Run(() =>
630628
{
631629
// prepare the information used to start the certutil process
632630
ProcessStartInfo psi = new ProcessStartInfo("certutil")
@@ -639,9 +637,9 @@ Task<string> generateHashAsync(string filePath, string algorithm)
639637
};
640638
string fileHash = string.Empty;
641639
string processOutput = string.Empty;
642-
using (Process p = new Process())
640+
using (Process p = new Process())
643641
{
644-
p.StartInfo = psi;
642+
p.StartInfo = psi;
645643
p.Start(); // start the certutil process
646644
processOutput = p.StandardOutput.ReadToEnd(); // read the output of the certutil process
647645
p.WaitForExit(); // wait for the process to exit
@@ -657,14 +655,52 @@ Task<string> generateHashAsync(string filePath, string algorithm)
657655
{
658656
MessageBox.Show("There was an error. Please try again");
659657
}
660-
tcs.SetResult(fileHash);
661-
}
662-
catch (Exception ex)
663-
{
664-
tcs.SetException(ex);
665-
}
666658

667-
return tcs.Task;
659+
return fileHash;
660+
});
661+
662+
663+
//// use a task completion source to run IO bound processes
664+
//TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
665+
//try
666+
//{
667+
// // prepare the information used to start the certutil process
668+
// ProcessStartInfo psi = new ProcessStartInfo("certutil")
669+
// {
670+
// Arguments = $"-hashfile \"{filePath}\" {algorithm}",
671+
// CreateNoWindow = true,
672+
// UseShellExecute = false,
673+
// RedirectStandardError = true, // to get the error from the process
674+
// RedirectStandardOutput = true // to get the output from the process
675+
// };
676+
// string fileHash = string.Empty;
677+
// string processOutput = string.Empty;
678+
// using (Process p = new Process())
679+
// {
680+
// p.StartInfo = psi;
681+
// p.Start(); // start the certutil process
682+
// processOutput = p.StandardOutput.ReadToEnd(); // read the output of the certutil process
683+
// p.WaitForExit(); // wait for the process to exit
684+
// }
685+
// if (!string.IsNullOrWhiteSpace(processOutput)) // check if the output form the certutil process is not empty
686+
// {
687+
// var lines = processOutput.Split(new[] { '\r', '\n' }); // split the output by lines
688+
// // the line that contains the file hash will not have any space in it
689+
// // so use that condition to get the file hash
690+
// fileHash = lines.FirstOrDefault(hash => !string.IsNullOrWhiteSpace(hash) && !hash.Contains(" "));
691+
// }
692+
// else
693+
// {
694+
// MessageBox.Show("There was an error. Please try again");
695+
// }
696+
// tcs.SetResult(fileHash);
697+
//}
698+
//catch (Exception ex)
699+
//{
700+
// tcs.SetException(ex);
701+
//}
702+
703+
//return tcs.Task;
668704
}
669705

670706
/// <summary>
@@ -681,7 +717,7 @@ Task<bool> isFileHashValidAsync(string filePath, string fileHash, string algorit
681717
{
682718
ProcessStartInfo psi = new ProcessStartInfo("certutil")
683719
{
684-
Arguments = $"-hashfile {filePath} {algorithm}",
720+
Arguments = $"-hashfile \"{filePath}\" {algorithm}",
685721
CreateNoWindow = true,
686722
UseShellExecute = false,
687723
RedirectStandardError = true,
@@ -699,7 +735,7 @@ Task<bool> isFileHashValidAsync(string filePath, string fileHash, string algorit
699735
if (!string.IsNullOrWhiteSpace(processOutput))
700736
{
701737
var lines = processOutput.Split(new[] { '\r', '\n' });
702-
isValid = lines.Any(hash => string.Equals(hash, fileHash, StringComparison.OrdinalIgnoreCase));
738+
isValid = lines.Any(hash => string.Equals(hash.ToLower(), fileHash.ToLower(), StringComparison.OrdinalIgnoreCase));
703739
}
704740
else
705741
{

0 commit comments

Comments
 (0)