简体   繁体   English

如何在列表框中显示字符串的哈希?

[英]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: 我有一个项目,我必须创建自己的线性哈希,存储桶哈希和二次哈希算法,所以我选择对一些字符串进行哈希处理,但是遇到一个小问题,我有2个ListBoxes:

  • A ListBox full of strings 一个充满字符串的列表框
  • A ListBox full of the hashes of those strings 包含这些字符串的哈希值的ListBox

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. 我可以毫无问题地打印字符串,但是当我单击按钮时,我不知道如何将它们的对应哈希值打印到另一个ListBox中。

Here's my code: 这是我的代码:

Form.cs 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 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? 为什么Hash.cs中的foreach返回false?

I solved my issue by converting the Byte[] using BitConverter.ToString(hashToConvert).Replace("-", string.Empty); 我通过使用BitConverter.ToString(hashToConvert).Replace("-", string.Empty);转换Byte []解决了我的问题BitConverter.ToString(hashToConvert).Replace("-", string.Empty); .

Hash.cs 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM