0

Hi I have written a class to create a hash for a String input but my Program sometimes give same hash for two different input.

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class Test {

public byte[] Hash(String input) throws NoSuchAlgorithmException
{
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    byte b[] = messageDigest.digest(input.getBytes());
    return b;
}

public static void main(String args[]) throws NoSuchAlgorithmException
{
   Test t = new Test();
   byte[] hashValue = t.Hash("viud");
   String hashString = hashValue.toString();
   while(hashString.length()<32)
   {
       hashString = "0" + hashString;
   }
   System.out.println(hashString);
}

}

When my input to the function Hash() is "viud" the I am getting result as --> 0000000000000000000000[B@13e8c1c And when my input String is "Hello" then also I am getting result as --> 0000000000000000000000[B@13e8c1c

But this case is happening only few times on program execution. Every time I am running the Program,I am getting different hash generated for the same input value and also sometimes getting same hash value for two different inputs.

What happens exactly??

1 Answers1

1
   byte[] hashValue = t.Hash("viud");
   String hashString = hashValue.toString();

toString on a byte[] will give you the memory (heap) address of the byte[]. This isn't what you want. You want

String hashString = new String(t.Hash("viud"));
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • using String hashString = new String(t.Hash("viud")) will give me Unique hash for every input? @ControlAltDel – Vidhi Agarwal Nov 01 '19 at 13:26
  • 2
    @VidhiAgarwal No, hashes can't be unique because you are mapping an infinite number of inputs to a sequence of 16 bytes (for MD-5). – Alex R Nov 01 '19 at 13:28
  • @VidhiAgarwal No. No hash code algorithm produces unique hashes. – ControlAltDel Nov 01 '19 at 13:28
  • I tries your soln but getting output now as -- 0000000000000000]A@*¼K*v¹q?‘Å’ @ControlAltDel – Vidhi Agarwal Nov 01 '19 at 13:29
  • @VidhiAgarwal you may have to add a second parameter to new String(...) to define the text format (i.e. ascii, utf-8/16, latin-1, etc) – ControlAltDel Nov 01 '19 at 13:30
  • If hashes can't be unique then what is the importance of hashes? @AlexR – Vidhi Agarwal Nov 01 '19 at 13:31
  • Is there a way to generate unique hash for ever input? I want to use hash for login page for secure login. @AlexR – Vidhi Agarwal Nov 01 '19 at 13:33
  • 1
    @VidhiAgarwal For that, you should use a so called _salt_. You could read [this article](https://crackstation.net/hashing-security.htm). And if you really want to do that, you probably shouldn't use MD5.. – Alex R Nov 01 '19 at 13:36
  • @VidhiAgarwal MD5 can be used for the same purposes that checksum used to be used for – ControlAltDel Nov 01 '19 at 14:42
  • @VidhiAgarwal If the requirement is that the hash must be unique, compressing the document using gzip is information-wise about as good as you are going to get – ControlAltDel Nov 01 '19 at 16:03