private void btnBrowse_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog() { CheckFileExists = false }; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (ValidateInput(ofd.FileName)) { _file = new FileInfoEx(ofd.FileName); tbFile.Text = _file.FullName; gbAlgorithmSingle.IsEnabled = true; gbResult.IsEnabled = true; HashFile(_file, gAlgorithmSingle); tbResult.Text = _file.Hash.Hash; } else { MessageBox.Show("File " + ofd.FileName + " does not exist. Please select a valid file.", "File Error", MessageBoxButton.OK, MessageBoxImage.Error); gbAlgorithmSingle.IsEnabled = false; gbResult.IsEnabled = false; } } }
private void tbFile_LostFocus(object sender, RoutedEventArgs e) { if (ValidateInput(tbFile.Text)) { _file = new FileInfoEx(tbFile.Text); gbAlgorithmSingle.IsEnabled = true; gbResult.IsEnabled = true; HashFile(_file, gAlgorithmSingle); tbResult.Text = _file.Hash.Hash; } else { gbAlgorithmSingle.IsEnabled = false; gbResult.IsEnabled = false; if (string.IsNullOrWhiteSpace(tbFile.Text)) { return; } MessageBox.Show("File " + tbFile.Text + " does not exist. Please select a valid file.", "File Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void HashFile(FileInfoEx file, Grid gAlgorithm) { foreach (UIElement uie in gAlgorithm.Children) { if (uie is RadioButton) { RadioButton rbTemp = (RadioButton)uie; if (rbTemp.IsChecked.Value) { if (rbTemp.Content.ToString().ToLower().Equals("md5")) { file.Hash = new Checksum(algo.MD5, Cryptographic.CalculateMD5(File.ReadAllBytes(file.FullName))); } if (rbTemp.Content.ToString().ToLower().Equals("sha1")) { file.Hash = new Checksum(algo.SHA1, Cryptographic.CalculateSHA1(File.ReadAllBytes(file.FullName))); } if (rbTemp.Content.ToString().ToLower().Equals("sha2")) { file.Hash = new Checksum(algo.SHA2, Cryptographic.CalculateSHA2(File.ReadAllBytes(file.FullName))); } if (rbTemp.Content.ToString().ToLower().Equals("sha3")) { file.Hash = new Checksum(algo.SHA3, Cryptographic.CalculateSHA3(File.ReadAllBytes(file.FullName))); } if (rbTemp.Content.ToString().ToLower().Equals("crc32")) { file.Hash = new Checksum(algo.CRC32, Cryptographic.CalculateCRC32(File.ReadAllBytes(file.FullName))); } if (rbTemp.Content.ToString().ToLower().Equals("crc64")) { file.Hash = new Checksum(algo.CRC64, Cryptographic.CalculateCRC64(File.ReadAllBytes(file.FullName))); } } } } }