I am trying to practice java cryptography, but I noticed when I try to get the byte array of certain strings the byte array is always the same, why is this?
Here is my code snippet :
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Main {
public static void main(String[] args) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
String message = "you are welcome";
messageDigest.update(message.getBytes());
byte[] md = messageDigest.digest();
System.out.println(md);
} catch (NoSuchAlgorithmException e) {
System.out.println("no such algorithm");
e.printStackTrace();
}
}
}
and no matter the value of my message variable, the result always printed out is [B@1540e19d
but what makes me more curious is when I convert my byte[] md into hex values,
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Main {
public static void main(String[] args) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
String message = "thank you";
messageDigest.update(message.getBytes());
byte[] md = messageDigest.digest();
System.out.println(md);
//Converting the byte array in to HexString format
StringBuffer hexString = new StringBuffer();
for (int i = 0;i< md.length;i++) {
hexString.append(Integer.toHexString(0xFF & md[i]));
}
System.out.println("Hex format : " + hexString.toString());
} catch (NoSuchAlgorithmException e) {
System.out.println("no such algorithm");
e.printStackTrace();
}
}
}
it produces totally different values for my StringBuffer hexString even the byte[] md is the same for two different strings. Please, could someone explain?