Beispiel #1
0
        private bool IsHashValid()
        {
            // first, validate the actual hash chosen
            bool valid = comboBoxHash.Items.Contains(comboBoxHash.Text);

            // second, for keyed hash algorithms only, validate the key
            using (System.Security.Cryptography.HashAlgorithm hash = System.Security.Cryptography.HashAlgorithm.Create(comboBoxHash.Text))
            {
                if (hash != null)
                {
                    if (hash.GetType().IsSubclassOf(typeof(System.Security.Cryptography.KeyedHashAlgorithm)))
                    {
                        byte[] key = System.Text.Encoding.ASCII.GetBytes(textBoxHashKey.Text);

                        // MACTripleDES is a special case that only accepts a key that is 16 or 20 bytes long
                        // and must not be "weak" as determined by the library
                        if (hash is System.Security.Cryptography.MACTripleDES)
                        {
                            valid = (key.Length == 16 || key.Length == 24) && !System.Security.Cryptography.TripleDES.IsWeakKey(key);
                        }
                        // all other keyed hashes only require non empty keys
                        else
                        {
                            valid = !string.IsNullOrEmpty(textBoxHashKey.Text);
                        }
                    }
                }
                else
                {
                    valid = false;
                }
            }

            return(valid);
        }
Beispiel #2
0
        //TODO: probably use elsewhere but careful of the using
        private bool IsHashAlgorithm(string hash)
        {
            bool isHashAlgorithm = false;

            // set the state of the key text box based on whether a keyed hash has been selected
            using (System.Security.Cryptography.HashAlgorithm hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hash))
            {
                //System.Security.Cryptography.KeyedHashAlgorithm keyedHash = hash as System.Security.Cryptography.KeyedHashAlgorithm;
                //if (keyedHash != null)  //TODO: test performance
                isHashAlgorithm = (hashAlgorithm.GetType().IsSubclassOf(typeof(System.Security.Cryptography.KeyedHashAlgorithm)));
            }

            return(isHashAlgorithm);
        }
Beispiel #3
0
        private void HashBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Do not access the form's BackgroundWorker reference directly.
            // Instead, use the reference provided by the sender parameter.
            BackgroundWorker bw = sender as BackgroundWorker;

            List <object> arguments = e.Argument as List <object>;

            List <string> filesToHash = new List <string>();

            // retrieve all files to hash from user input
            string[] list = ((string)arguments[0]).Split(';');
            foreach (string fileOrDirectory in list)
            {
                if (System.IO.Directory.Exists(fileOrDirectory))
                {
                    filesToHash.AddRange(System.IO.Directory.GetFiles(fileOrDirectory.Trim(), "*", (System.IO.SearchOption)arguments[3]));
                }
                else if (System.IO.File.Exists(fileOrDirectory))
                {
                    filesToHash.Add(fileOrDirectory.Trim());
                }
            }

            // report the number of files to support the progress bar limits
            bw.ReportProgress(0, filesToHash.Count);

            using (System.Security.Cryptography.HashAlgorithm hash = System.Security.Cryptography.HashAlgorithm.Create((string)arguments[1]))
            {
                // if using a keyed hash, set the key before computing the hash
                if (hash.GetType().IsSubclassOf(typeof(System.Security.Cryptography.KeyedHashAlgorithm)))
                {
                    byte[] key = System.Text.Encoding.ASCII.GetBytes((string)arguments[2]);
                    ((System.Security.Cryptography.KeyedHashAlgorithm)hash).Key = key;
                }

                foreach (string file in filesToHash)
                {
                    if (bw.CancellationPending)
                    {
                        hashResults.Add(file, "Cancelled");
                        e.Cancel = true;
                        break;
                    }

                    hashResults.Add(file, HashFile(file, hash));
                    bw.ReportProgress(0);
                }
            }
        }