Reputation: 1684
So for a certain task I was asked to find collisions for the first n-bits (n will be 4, 8 ...) of a SHA256
digest between 2 random 64 byte sequence, which I do viz.
SHA256Managed hashstring = new SHA256Managed();
byte[] hash_1 = hashstring.ComputeHash(a);
...
I compute the string value for the hashes viz.
string hashString = string.Empty;
foreach (byte x in hash)
{
hashString += String.Format("{0:x2}", x);
}
return hashString;
And I use the string matching function to first extract, say the first 8 bits:
string first8Char_hash1 = new string(hashDisplay(hash_1).Take(8).ToArray());
check if the first say, 8 bits match or not, viz.
if (first8Char_hash1.Equals(first8Char_hash2))
{
//Do something
}
Now, if I do, say the first 4 bits, I find a collision within say an average 12,000 iterations, however, for the first 8 bits, well I am unable to find a collision even after 1,00,00 tries.
What's the problem? Thanks.
Upvotes: 0
Views: 271
Reputation: 726987
When you print a number as hex, each 4 bits become one hex character. If you would like to find collisions among the first 4 bits, you compare one character, not four. Four characters correspond to 16-bit collisions.
For eight bits, compare the first two characters of the hex string.
Upvotes: 2