示例#1
0
        private void ComputeHashingButton_Click(object sender, EventArgs e)
        {
            AlgorithmDataDisplayErrorLabel.Visible = false;
            ComputeHashingButton.Enabled           = false;

            if (!string.IsNullOrEmpty(PlainTextTextBox.Text))
            {
                var    selectedItem = AlgorithComboBox.SelectedItem;
                byte[] generatedKey = ComputeService.GenerateKey();

                if (selectedItem is HashingModel model)
                {
                    GeneratedKeyTextBox.Text = Convert.ToBase64String(generatedKey);
                    var hashedResult = new object();

                    switch (model.HashingType)
                    {
                    case HashingType.SHA1:
                        StopWatch.Start();
                        hashedResult = ComputeService.ComputeSHA1Hash(Encoding.UTF8.GetBytes(PlainTextTextBox.Text));
                        StopWatch.Stop();
                        Time = StopWatch.Elapsed;
                        StopWatch.Reset();

                        DisplayBenchmarks(HashingType.SHA1);
                        break;

                    case HashingType.SHA256:
                        StopWatch.Start();
                        hashedResult = ComputeService.ComputeSHA256Hash(Encoding.UTF8.GetBytes(PlainTextTextBox.Text));
                        StopWatch.Stop();
                        Time = StopWatch.Elapsed;
                        StopWatch.Reset();

                        DisplayBenchmarks(HashingType.SHA256);
                        break;

                    case HashingType.SHA384:
                        StopWatch.Start();
                        hashedResult = ComputeService.ComputeSHA384Hash(Encoding.UTF8.GetBytes(PlainTextTextBox.Text));
                        StopWatch.Stop();
                        Time = StopWatch.Elapsed;
                        StopWatch.Reset();

                        DisplayBenchmarks(HashingType.SHA384);
                        break;

                    case HashingType.SHA512:
                        StopWatch.Start();
                        hashedResult = ComputeService.ComputeSHA512Hash(Encoding.UTF8.GetBytes(PlainTextTextBox.Text));
                        StopWatch.Stop();
                        Time = StopWatch.Elapsed;
                        StopWatch.Reset();

                        DisplayBenchmarks(HashingType.SHA512);
                        break;

                    case HashingType.HMAC:
                        StopWatch.Start();
                        hashedResult = ComputeService.ComputeHMACHash(Encoding.UTF8.GetBytes(PlainTextTextBox.Text), generatedKey);
                        StopWatch.Stop();
                        Time = StopWatch.Elapsed;
                        StopWatch.Reset();

                        DisplayBenchmarks(HashingType.HMAC);
                        break;

                    default:
                        break;
                    }

                    SetDisplayProperties((byte[])hashedResult);
                }

                ComputeHashingButton.Enabled = true;
            }
            else
            {
                ComputeHashingButton.Enabled           = true;
                AlgorithmDataDisplayErrorLabel.Text    = "*Please enter something to hash";
                AlgorithmDataDisplayErrorLabel.Visible = true;
            }
        }