使用相同的哈希码生成两个不同的字符串

M.k*_*ary 31 c# string hashcode

我想做一些测试,这些测试需要一些具有相同哈希码的字符串,但不是相同的字符串.我找不到任何例子,所以我决定写一个简单的程序来为我做.

下面的代码反复生成两个随机字符串,直到它们生成相同的哈希码.

    static Random r = new Random();
    static void Main(string[] args)
    {
        string str1, str2;
        do
        {
            str1 = GenerateString();
            str2 = GenerateString();
        } while (str1.GetHashCode() != str2.GetHashCode() && str1 != str2);

        Console.WriteLine("{0}\n{1}", str1, str2);
    }

    static string GenerateString()
    {
        string s = "";
        while (s.Length < 6)
        {
            s += (char)r.Next(char.MaxValue);
        }
        return s;
    }
Run Code Online (Sandbox Code Playgroud)

这段代码似乎有效(理论上),但可能需要几个世纪才能完成.所以我反过来考虑从一个哈希码生成两个字符串.

我知道从哈希码中检索字符串是不可能的,但是可以从中生成可能的字符串吗?

我正在使用Visual Studio 2015社区版.版本:14.0.23107.0D14REL.

.NET Framework:4.6.00081.

Sam*_*eff 32

通过反复比较随机字符串来找到两个字符串几乎是永远的.而是生成字符串并通过哈希码将它们存储在字典中.然后查找每个.匹配发现很快.

匹配发现!! xqzrbn和krumld哈希码80425224

void Main()
{

    var lookup = new Dictionary<int,string>();

    while(true) {
        var s = RandomString();        
        var h = s.GetHashCode();
        string s2;
        if (lookup.TryGetValue(h, out s2) && s2 != s) {
            Console.WriteLine("MATCH FOUND!! {0} and {1} hash code {2}",
                lookup[h],
                s,
                h);
            break;
        }
        lookup[h] = s;

        if (lookup.Count % 1000 == 0) {
            Console.WriteLine(lookup.Count);
        }
    }
}

static Random r = new Random();

// Define other methods and classes here
static string RandomString() {

    var s = ((char)r.Next((int)'a',((int)'z')+1)).ToString() +
            ((char)r.Next((int)'a',((int)'z')+1)).ToString() +
            ((char)r.Next((int)'a',((int)'z')+1)).ToString() +
            ((char)r.Next((int)'a',((int)'z')+1)).ToString() +
            ((char)r.Next((int)'a',((int)'z')+1)).ToString() +
            ((char)r.Next((int)'a',((int)'z')+1)).ToString();

    return s;
}
Run Code Online (Sandbox Code Playgroud)


Sco*_*ain 25

利用生日悖论.而不是直接测试两个字符串,测试之前看到的所有字符串.

using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new Dictionary<int, string>();

            int i = 0;
            string teststring;
            while (true)
            {
                i++;
                teststring = i.ToString();
                try
                {
                    words.Add(teststring.GetHashCode(), teststring);
                }
                catch (Exception)
                {
                    break;
                }
            }

            var collisionHash = teststring.GetHashCode();
            Console.WriteLine("\"{0}\" and \"{1}\" have the same hash code {2}", words[collisionHash], teststring, collisionHash);
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于我的机器,它产生输出

"699391"和"1241308"具有相同的哈希码-1612916492

几乎是瞬间.

由于在.NET中如何对字符串进行哈希处理,您可能无法获得与我完全相同的输出,但它应该同样快.