SHA1 collision for first 32 bits for two different messages Is it possible to create a program to find the collision of 2 different messages with the same first 32 bits using sha1 hashing? I tried using a while loop, but it ran endlessly as though no match were found. I am doing this in Java. These 2 messages must contain "abc123". 

My while logic is adding a number to the end of the string, but it fails.

Below is my sha1 hashing (let's just call it) pseudo-code:

public static String sha1Hashing (String message) {
    String sha1 = "";
    StringBuffer sb = new StringBuffer();
    try {
        MessageDigest mDigest = MessageDigest.getInstance("SHA1");
        byte[] result = mDigest.digest(message.getBytes());

        for (int i = 0; i &lt; result.length; i++) {
            sb.append(Integer.toString((result[i] &amp; 0xff) + 0x100, 16).substring(1));
        }
    } catch(NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    //return first 32 char
    sha1 = sb.toString().substring(0,32);
    return sha1;
}
To find a collision it would be best to perform a birthday attack (as I already commented below the question).

You could use abc123 as prefix and then append a counter (using any encoding), creating the message to hash. Then you'd calculate a hash over the message and take the first 4 bytes. You'd create a map using the 4 byte hash as key (e.g. encoded as hexadecimals) and the message.

Now you simply add these key / value pairs until you find a duplicate key. In that case you print out the current message and the message in the map, and - of course - the hash collision that you found.

Because of the birthday problem you'd expect to find a match in less than a second and minimal memory use.



Proof:

Found match: abc123_owlstead_1255 and abc123_owlstead_59131 -&gt; 992156E0
That took approximately 430 milli seconds 


Note that if you post this as an answer, the word "owlstead" should raise some questions and google will quickly find this answer. So I would not copy paste this answer if I were you.