Reputation: 2095
I'm using Java's MessageDigest to do a hashing project. I want to create collisions with hashes made by randomly generated strings. I have verified that my strings are truly random. When I output the digest however it always begins with "[B@" for some reason and when I'm trying to detect a collision with 8 bits obviously everything starts with "[". Here is my code:
public boolean encrypt(String x) throws Exception {
System.out.println("x is " + x);
java.security.MessageDigest d = java.security.MessageDigest.getInstance("SHA-1");
d.update(x.getBytes());
d.reset();
String result = d.digest().toString() + " ";
char[] tempCharArray = result.toCharArray();
String bitArray = "";
for(int i=0; i< tempCharArray.length; i++){
bitArray += String.format("%8s", Integer.toBinaryString(tempCharArray[i] &
0xff)).replace(' ', '0');
}
result = bitArray.substring(0,8);
return result;
}
Has anyone seen this before/ know what to do to get it right? Thanks
Upvotes: 0
Views: 687
Reputation: 18445
You look to be doing some strange stuff in your code.
First, you call;
String result = d.digest().toString()
..which is going to give you the string representation of a byte array object, which is made of the class name, an "@" symbol, and the hashcode. Arrays have a class name of "[B", hence you will always get something starting "[B@".
Secondly you call d.update(x.getBytes())
and then immediately call d.reset()
. Even if you fix the first problem you are digesting nothing, irrespective of the value of x
, so you will always get the same result, the SHA-1 hash of an empty string, which is da39a3ee5e6b4b0d3255bfef95601890afd80709
.
Upvotes: 2