Exemplo n.º 1
0
        private async void HashProcessing(string fileToHash)
        {
            btBrowse.Enabled = btMD5.Enabled = btSHA1.Enabled = btSHA256.Enabled = btSHA512.Enabled = false;
            tbMD5.Text       = tbSHA1.Text = tbSHA256.Text = tbSHA512.Text = tbTimeToCalculateMD5.Text = tbTimeToCalculateSHA1.Text = tbTimeToCalculateSHA256.Text = tbTimeToCalculateSHA512.Text = string.Empty;
            UpdateForm(fileToHash);

            long totalElapsed = 0;

            totalResult = fileWithHash = string.Empty;

            tsStatus.Text = "Création des empreintes en cours...";
            try { totalElapsed = await GetHashes(fileToHash); }
            catch (Exception) { MessageBox.Show("Une erreur s'est produite lors du calcul de l'empreinte", "Erreur : Calcul de l'empreinte", MessageBoxButtons.OK, MessageBoxIcon.Error); }

            totalResult   = ValuesTools.elapsedToString(totalElapsed);
            tsStatus.Text = string.Format("Création des empreintes terminée en {0} - Vérification en cours...", totalResult);

            KeyValuePair <string, string> results = new KeyValuePair <string, string>(string.Empty, string.Empty);

            try { results = HashToolkit.VerifyFile(fileToHash, tbMD5.Text, tbSHA1.Text, tbSHA256.Text, tbSHA512.Text); }
            catch (Exception) { MessageBox.Show("Une erreur s'est produite lors de la vérification de l'empreinte", "Erreur : vérification de l'empreinte", MessageBoxButtons.OK, MessageBoxIcon.Error); }

            string algoUsed = results.Key;

            fileWithHash  = results.Value;
            tsStatus.Text = (string.IsNullOrEmpty(algoUsed)) ?
                            string.Format("Création des empreintes terminée en {0} : aucun fichier d'empreinte trouvé", totalResult) :
                            string.Format("Création des empreintes terminée en {0}  : fichier conforme à son empreinte {1}", totalResult, algoUsed);

            btBrowse.Enabled = true;
            UpdateForm(fileToHash);
        }
Exemplo n.º 2
0
        private async Task <long> GetHashes(string fileToHash)
        {
            long      totalElapsed = 0;
            Stopwatch sw           = new Stopwatch();

            sw.Start();
            tbMD5.Text = await Task.Run(() => HashToolkit.GetHash(HashToolkit.Algorithms.MD5, fileToHash));

            sw.Stop();

            totalElapsed += sw.ElapsedMilliseconds;
            tbTimeToCalculateMD5.Text = string.Format("{0}", ValuesTools.elapsedToString(sw.ElapsedMilliseconds));

            sw.Restart();
            tbSHA1.Text = await Task.Run(() => HashToolkit.GetHash(HashToolkit.Algorithms.SHA1, fileToHash));

            sw.Stop();

            totalElapsed += sw.ElapsedMilliseconds;
            tbTimeToCalculateSHA1.Text = string.Format("{0}", ValuesTools.elapsedToString(sw.ElapsedMilliseconds));

            sw.Restart();
            tbSHA256.Text = await Task.Run(() => HashToolkit.GetHash(HashToolkit.Algorithms.SHA256, fileToHash));

            sw.Stop();

            totalElapsed += sw.ElapsedMilliseconds;
            tbTimeToCalculateSHA256.Text = string.Format("{0}", ValuesTools.elapsedToString(sw.ElapsedMilliseconds));

            sw.Restart();
            tbSHA512.Text = await Task.Run(() => HashToolkit.GetHash(HashToolkit.Algorithms.SHA512, fileToHash));

            sw.Stop();

            totalElapsed += sw.ElapsedMilliseconds;
            tbTimeToCalculateSHA512.Text = string.Format("{0} s", ValuesTools.elapsedToString(sw.ElapsedMilliseconds));

            return(totalElapsed);
        }