C#, write a method :
public static List<string> FindStringsWithMatchingHashcodes()
that will produce 3 different System.String objects (different string values) that have the same hash code such that none of the resulting String objects have the same value.
Note 1: you dont need to know GetHashCode implementation details, you dont need to override GetHashCode anywhere and you must not create your own String classes.
Note 2: Only strings with displayable characters will be considered.
Note 3: the usage of multithreading is not required for this exercise.
public static List<string> FindStringsWithMatchingHashcodes()
that will produce 3 different System.String objects (different string values) that have the same hash code such that none of the resulting String objects have the same value.
Note 1: you dont need to know GetHashCode implementation details, you dont need to override GetHashCode anywhere and you must not create your own String classes.
Note 2: Only strings with displayable characters will be considered.
Note 3: the usage of multithreading is not required for this exercise.
using System;
using System.Collections.Generic;
public class HashCollisionFinder
{
public static List<string> FindStringsWithMatchingHashcodes()
{
var hashDict = new Dictionary<int, List<string>>();
var passedStr = new HashSet<string>();
var rand = new Random();
int length = rand.Next(3, 15);
while (true)
{
string candidate = GenerateRandomStr(rand, 4);
if (!passedStr.Add(candidate))
continue;
int hash = candidate.GetHashCode();
if (!hashDict.TryGetValue(hash, out var list))
{
list = new List<string>();
hashDict[hash] = list;
}
list.Add(candidate);
if (list.Count == 3)
return list;
}
}
private static string GenerateRandomStr(Random rand, int length)
{
char[] buffer = new char[length];
for (int i = 0; i < length; i++)
{
buffer[i] = (char)rand.Next(32, 127); // characters from ASCII table
}
return new string(buffer);
}
public static void Main()
{
List<string> result = FindStringsWithMatchingHashcodes();
Console.WriteLine("Three different strings with the same hash code:");
foreach (string str in result)
{
Console.WriteLine($"{str} -> HashCode: {str.GetHashCode()}");
}
}
}
using System.Collections.Generic;
public class HashCollisionFinder
{
public static List<string> FindStringsWithMatchingHashcodes()
{
var hashDict = new Dictionary<int, List<string>>();
var passedStr = new HashSet<string>();
var rand = new Random();
int length = rand.Next(3, 15);
while (true)
{
string candidate = GenerateRandomStr(rand, 4);
if (!passedStr.Add(candidate))
continue;
int hash = candidate.GetHashCode();
if (!hashDict.TryGetValue(hash, out var list))
{
list = new List<string>();
hashDict[hash] = list;
}
list.Add(candidate);
if (list.Count == 3)
return list;
}
}
private static string GenerateRandomStr(Random rand, int length)
{
char[] buffer = new char[length];
for (int i = 0; i < length; i++)
{
buffer[i] = (char)rand.Next(32, 127); // characters from ASCII table
}
return new string(buffer);
}
public static void Main()
{
List<string> result = FindStringsWithMatchingHashcodes();
Console.WriteLine("Three different strings with the same hash code:");
foreach (string str in result)
{
Console.WriteLine($"{str} -> HashCode: {str.GetHashCode()}");
}
}
}
Aucun commentaire:
Enregistrer un commentaire