Search code examples
c#winformshashstring-hashing

How do I show the hash of a string in a ListBox?


I have a project where I have to create my own linear hash, bucket hash and quadratic hash algorithms, so I choose to hash some strings, but I've run into a little problem where I have 2 ListBoxes:

  • A ListBox full of strings
  • A ListBox full of the hashes of those strings

I can print the strings without any problems, but I can't figure out how to print their corresponding hashes into the other ListBox when I click on a button.

Here's my code:

Form.cs

private void btnLinearHash_Click(object sender, EventArgs e)
{
    // Initialize Hash.cs (see below for code)
    Hash.LinearHash linearHash = new Hash.LinearHash();
    RandomArrayGen randGen = new RandomArrayGen();

    // Clear both ListBoxes
    lbHashStrings.Items.Clear();
    lbHashesOfStrings.Items.Clear();

    for (int i = 0; i < 12; i++)
    {
        // Generate 12 random upper and lowercase strings using RandomArrayGen.cs (see https://pastebin.com/jRsG5r1E)
        lbHashStrings.Items.Add(randGen.GenRandomStrings(12, true, true));
    }

    for (int i = 0; i < lbHashStrings.Items.Count; i++)
    {
    // Generate a hash of a string and store it into a Dictionary (see Hash.cs)
    linearHash.GenerateHashOfString(lbHashStrings.Items[i].ToString());
    lbHashesOfStrings.Items.Add(linearHash.FindHashCodeInDictionary(lbHashStrings.Items[i].ToString()));
    }
}

Hash.cs

    public class LinearHash
    {
        private Dictionary<byte[], string> linearHashArray = new Dictionary<byte[], string>();
        private byte[] hashCode;

        public void GenerateHashOfString(string stringToHash)
        {
            hashCode = Encoding.ASCII.GetBytes(stringToHash);

            linearHashArray.Add(hashCode, stringToHash);
        }

        public string FindHashCodeInDictionary(string StringToFind)
        {
            byte[] HashOfStringToFind = Encoding.ASCII.GetBytes(StringToFind);

            //For some reason, this keeps returning false
            foreach (var keyPairValue in linearHashArray)
            {
                if ((linearHashArray.ContainsKey(HashOfStringToFind) && linearHashArray.ContainsValue(StringToFind))
                {
                    return Encoding.UTF8.GetString(HashOfStringToFind);
                }
            }
            return "Hash not found";
        }
    }

So, my question is: What am I doing wrong? Why is the foreach in Hash.cs returning false?


Solution

  • I solved my issue by converting the Byte[] using BitConverter.ToString(hashToConvert).Replace("-", string.Empty);.

    Hash.cs

        public void GenerateHashOfString(string stringToHash)
        {
            hashCode = Encoding.ASCII.GetBytes(stringToHash);
        ==> hashCodeInStringForm = BitConverter.ToString(hashCode).Replace("-", string.Empty);
    
            linearHashArray.Add(hashCode, stringToHash);
        }
    
        public string FindHashCodeInDictionary(string StringToFind)
        {
        byte[] HashOfStringToFind = Encoding.ASCII.GetBytes(StringToFind);
    
        foreach (var hashCode in linearHashArray)
        {
            if (!linearHashArray.ContainsKey(HashOfStringToFind) && !linearHashArray.ContainsValue(StringToFind))
            {
            return "String and hash not found in array";
            }
        }
        return hashCodeInStringForm;
        }