1

I have written a code to generate a MD5 but unfortunately its generating a new MD5 every time for the same string. Can anyone please help.

Code is like below :

public static byte[] getHash(String[] constants)
{
MessageDigest md= MessageDigest.getInstance("MD5");
StringBuilder toBeHashed=new StringBuidler();
for(String c: constants)
{
toBeHashed.append(c);
}
return md.digest(toBeHashed.toString().getBytes());
}

Driver code :

byte[] hash=MyClass.getHash(new String[] {"01L488213P9579","2021-31-31"});

Can anyone please help and let me know if the code I wrote is correct or not? Is it because of the new String array, I am passing every time ?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • 1
    Are you sure you’re passing the same String each time? Please provide code that shows the issue, so we can reproduce it. That is, provide a function that calls getHash, and gives different output for the same String. – Max Jul 20 '21 at 20:44
  • yes I am using the same string, my String is "01L488213P95792021-31-31" – Searching For Solutions Jul 20 '21 at 20:48
  • Again, please attach the code that shows the problem, the full input and the full output. – Max Jul 20 '21 at 20:53
  • Hi added the code, pls check now.. – Searching For Solutions Jul 20 '21 at 20:53
  • SO how to createmd5 hash for the string array ? I am just printing hash in my driver code, its coming like B@12.. something , new everytime – Searching For Solutions Jul 20 '21 at 20:57
  • Your code won't compile there is typo in `StringBuidler`. Post the actual code that doesn't work for you, your current example after fixing typo works and generates the same hash. – Karol Dowbecki Jul 20 '21 at 20:57
  • Okay, you're still only hashing one string once. And no output that i can see? – Max Jul 20 '21 at 20:57
  • B@12 is not a hash. That's the address of the byte array. You can't just do System.out.println(bytearray), because it's not a String. It just calls [].toString which defaults to an internal representation of the byte array. – Max Jul 20 '21 at 20:58
  • @KarolDowbecki code is compling fine, I just wrote it manually here, cant copy paste the actual code.. – Searching For Solutions Jul 20 '21 at 20:58
  • @Max, Ohh right you are, actually I want to see the hash which is created.. how to do it then? I need to save the MD5 hash as a KEY in HashMap, so pls guide me – Searching For Solutions Jul 20 '21 at 20:59

1 Answers1

4

The problem is here, when you say:

printing hash in my driver code, its coming like B@12.. something , new everytime

You are printing the array object which uses toString() method which for arrays shows the object hash code. It is unique for every array object so each time you get a new value.

You have to print the array content e.g. using java.util.Arrays:

System.out.println(java.util.Arrays.toString(hash));
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • This might be useful: [How to convert a byte array to a hex string in Java?](https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java) – Max Jul 20 '21 at 20:59