/// <summary> /// This method handles the initial validation of parameters for the HMAC genenaration process /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCalcHmac_Click(object sender, EventArgs e) { //Validate that the following values are available: //1) HMAC algorithm is selected //2) File //3) Key if (cmbHmacAlgo.Text == "--Select--" || String.IsNullOrEmpty(txtFileToHmac.Text) || String.IsNullOrEmpty(txtHmacKey.Text)) { MessageBox.Show("All the fields in RED must be selected!"); } else { // If all goes well, pass the values to the CalculateHMAC function in hashvalidator.ClassLibraries.calculator class // Too much input validation is intentionally avoided ClassLibraries.calculator objCalc = new ClassLibraries.calculator(); string HMACResult = objCalc.CalculateHMAC(txtFileToHmac.Text, cmbHmacAlgo.Text, RANDOM_KEY_BYTES); if (!String.IsNullOrEmpty(HMACResult) && !String.Equals(HMACResult, HMAC_ERROR)) { txtHmacValue.Text = HMACResult; } else { MessageBox.Show(HMACResult); } } }
private void btnValidateFileHmac_Click(object sender, EventArgs e) { //Validate that the following values are available: //1) HMAC algorithm is selected //2) File //3) Key if (cmbHmacAlgo.Text == "--Select--" || String.IsNullOrEmpty(txtFileToValidateHmac.Text) || String.IsNullOrEmpty(txtHmacKey.Text)) { MessageBox.Show("All the fields in RED must be selected!"); } else { //Convert the key to a byte array byte[] _KEY_BYTES = new byte[0]; // If all goes well, pass the values to the CalculateHMAC function in hashvalidator.ClassLibraries.calculator class // Too much input validation is intentionally avoided ClassLibraries.calculator objCalc = new ClassLibraries.calculator(); string HMACResult = objCalc.CalculateHMAC(txtFileToValidateHmac.Text, cmbHmacAlgo.Text, _KEY_BYTES); if (!String.IsNullOrEmpty(HMACResult) && !String.Equals(HMACResult, HMAC_ERROR)) { txtHmacValidationResult.Text = HMACResult; } else { MessageBox.Show(HMACResult); } } }
/// <summary> /// Calculates the Hash value based on the selected algorithm and file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCalculateHash_Click(object sender, EventArgs e) { //Get the hash algorithm selected by the user // if nothing selected show a message to select one // Same for file to calculate the hash of if (cbHashAlgorithms.Text == "--Select--") { MessageBox.Show("Select a hash algorithm"); } else if (String.IsNullOrEmpty(FILE_TO_EVAL)) { MessageBox.Show("Select a file to verify"); } else { //Everything is validated, so lets create an object of the Calculator Class ClassLibraries.calculator objCalculator = new ClassLibraries.calculator(); string[] returnedCalculation = objCalculator.CalculateHash(FILE_TO_EVAL, cbHashAlgorithms.Text.ToString()); if (string.Equals(returnedCalculation[0], "success")) { txtSimpleHashValue.Text = returnedCalculation[1]; //Enable the "Save to file" button btnSaveHashToFile.Enabled = true; } else //This block executes when their is an error { MessageBox.Show(returnedCalculation[0]); } } }
/// <summary> /// This method handles the user event on clicking the "Generate key" checkbox. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbGenerateKey_CheckedChanged(object sender, EventArgs e) { if (txtHmacKey.Text != String.Empty && cbGenerateKey.Checked != true) { MessageBoxButtons choices = MessageBoxButtons.YesNo; DialogResult userChoice = MessageBox.Show("Do you want to save the key?", "Please confirm?", choices); if (userChoice == DialogResult.Yes) { try { string fileName; //ToDo: Move all Save to file code to a seperate function SaveFileDialog objSaveFile = new SaveFileDialog(); objSaveFile.Title = "Select a location to save the key"; objSaveFile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; objSaveFile.FileName = "KEY"; objSaveFile.InitialDirectory = @"C:\"; if (objSaveFile.ShowDialog() == DialogResult.OK) { fileName = objSaveFile.FileName; File.WriteAllText(fileName, txtHmacKey.Text); txtHmacKey.Text = ""; } else { txtHmacKey.Text = ""; } } catch { MessageBox.Show("Error: Please try again!"); return; } } else { txtHmacKey.Text = ""; } } if (cbGenerateKey.Checked == true && txtHmacKey.Text == String.Empty) { ClassLibraries.calculator objGenKey = new ClassLibraries.calculator(); RANDOM_KEY_BYTES = objGenKey.GenerateRandomKey(); if (RANDOM_KEY_BYTES != null) { txtHmacKey.Text = BitConverter.ToString(RANDOM_KEY_BYTES).Replace("-", String.Empty).ToLower(); } else { MessageBox.Show(KEYGEN_ERROR_MSG); } } }
/// <summary> /// Start the hash calculation process. Triggers when the user clicks on /// "Validate the file" button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCalculateHash_Click(object sender, EventArgs e) { //Get the hash algorithm selected by the user // if nothing selected show a message to select one // Same for file to calculate the hash of if (cbHashAlgorithms.Text == "--Select--") { MessageBox.Show("Select a hash algorithm"); } else if (String.IsNullOrEmpty(FILE_TO_EVAL)) { MessageBox.Show("Select a file to verify"); } else if (String.IsNullOrEmpty(txtHashValue.Text)) { MessageBox.Show("Provide a pre-calculated hash to verify against"); } else { //Everything is validated, so lets create an object of the Calculator Class ClassLibraries.calculator objCalculator = new ClassLibraries.calculator(); string[] returnedCalculation = objCalculator.CalculateHash(FILE_TO_EVAL, cbHashAlgorithms.Text.ToString()); if (string.Equals(returnedCalculation[0], "success")) { //if (String.IsNullOrEmpty(txtHashValue.Text)) //{ // txtResults.Text = returnedCalculation[1]; //} //else if (String.Equals(returnedCalculation[1], txtHashValue.Text)) if (String.Equals(returnedCalculation[1], txtHashValue.Text)) { txtResults.BackColor = System.Drawing.Color.Green; txtResults.Text = "Valid File!!!"; } else { txtResults.BackColor = System.Drawing.Color.Red; txtResults.Text = "Invalid File!!"; } } else //This block executes when their is an error { MessageBox.Show(returnedCalculation[1]); } } }