C
C#•5d ago
Yesn't

Hash collision checkin

I wanted to do some fun testing with SHA256 and ya how do i do collision checkin? 😛
29 Replies
Denis
Denis•5d ago
You can use a HashSet<T> or a Dictionary<TKey, TValue>. Add the created hash to these collections and when creating a new hash, check if said collections already contain it
Jimmacle
Jimmacle•5d ago
a collision just means 2 inputs produced the same hash so to check for them, check for duplicate hashes over your inputs
mtreit
mtreit•5d ago
You will never see a hash collision with SHA256 so this is kind of an irrelevant question.
Petris
Petris•5d ago
well that's not true every hash will have a collision at some point just for now there are no known ones for sha256
mtreit
mtreit•5d ago
I mean in practice. He was trying to find one. It's not going to happen for him.
Yesn't
Yesn'tOP•4d ago
well what about j4f ikr i just dont know how im gonna do it more than a while true and feed random stuff in i tried to do so, it ate my RAM very quickly so im just looking for suggestion from people, one might have better approach than me
Jimmacle
Jimmacle•4d ago
what is your end goal?
Yesn't
Yesn'tOP•4d ago
see if any collision appear, by accident
Jimmacle
Jimmacle•4d ago
but why
Yesn't
Yesn'tOP•4d ago
i dont actually know
Jimmacle
Jimmacle•4d ago
it's more likely that an asteroid will hit earth and kill you in the next second than you finding a sha256 collision
Yesn't
Yesn'tOP•4d ago
every hash will have a collision at some point
wanna find my luck
Jimmacle
Jimmacle•4d ago
i think there are more productive things to do as you've already found, you don't have the resources to do this in a meaningful way
Yesn't
Yesn'tOP•4d ago
does it have to really be meaningful?
Jimmacle
Jimmacle•4d ago
if you don't want to waste your time, yeah
Yesn't
Yesn'tOP•4d ago
i have too much time to waste just wanna invest some into a j4f project
Jimmacle
Jimmacle•4d ago
i mean if it's fun watching nothing happen and spiking your power bill go for it not sure what you need our help for
Yesn't
Yesn'tOP•4d ago
i want an efficient way more than while true and fill a hashset with results
Jimmacle
Jimmacle•4d ago
then try to find one
Yesn't
Yesn'tOP•4d ago
im finding one while waiting for an answer here i want different answers so i can learn something
Jimmacle
Jimmacle•4d ago
consider how many computers are mining bitcoin, which basically depends on the uniqueness of sha256 hashes all that compute hasn't found a hash collision yet
Yesn't
Yesn'tOP•4d ago
so you know im doing it for no real purpose i dont have a purpose im just doing it for fun
Jimmacle
Jimmacle•4d ago
but my point is, even for fun you can't do it you don't have the memory to store all the inputs and hashes you're creating
Yesn't
Yesn'tOP•4d ago
i know i cant but why dont just try it?
Jimmacle
Jimmacle•4d ago
i can't stop you, i can only tell you there's no point if it's fun for you it's fun for you
Yesn't
Yesn'tOP•4d ago
i absolutely know that there's absolutely no point
Jimmacle
Jimmacle•4d ago
well nobody will have better suggestions good luck i guess
GrabYourPitchforks
GrabYourPitchforks•4d ago
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Security.Cryptography;

BigInteger UPPER_BOUND = BigInteger.Pow(2, 256);
Dictionary<string, BigInteger> seenHashes = new();

for (BigInteger i = 0; i <= UPPER_BOUND; i++)
{
byte[] iHashed = SHA256.HashData(i.ToByteArray());
string iHashedAsString = BitConverter.ToString(iHashed);
if (!seenHashes.TryAdd(iHashedAsString, i))
{
Console.WriteLine($"Collision seen: {seenHashes[iHashedAsString]} and {i}");
return;
}
}
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Security.Cryptography;

BigInteger UPPER_BOUND = BigInteger.Pow(2, 256);
Dictionary<string, BigInteger> seenHashes = new();

for (BigInteger i = 0; i <= UPPER_BOUND; i++)
{
byte[] iHashed = SHA256.HashData(i.ToByteArray());
string iHashedAsString = BitConverter.ToString(iHashed);
if (!seenHashes.TryAdd(iHashedAsString, i))
{
Console.WriteLine($"Collision seen: {seenHashes[iHashedAsString]} and {i}");
return;
}
}
Ignoring pesky things like OOMs and random solar flares flipping bits of your computer's memory, this is guaranteed to find a collision. It's an implementation of Denis's idea at the top of the thread. We'll ignore for now that Dictionary can't hold 2^256 elements, since you'll OOM well ahead of that point. 😉 You'll see that I put an upper bound on this loop. Since each iteration of the loop increments i, we can hopefully all agree that this program will eventually terminate. Ignoring things like OOMs, solar flares, dictionary limitations, etc., here's a question left to the reader: is this program guaranteed to hit the Console.WriteLine block before it terminates, or might it terminate without writing anything to the console (by simply exhausting the loop condition)?
mtreit
mtreit•4d ago
It should be guaranteed to print a collision before the loop terminates, no?

Did you find this page helpful?