I am trying to get a hash value for a string using MessageDigest in Java, but the value is different every time. When I run the program twice it will again have completely different answers.
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException nsae) {
}
md.update("string".getBytes());
byte[] digest = md.digest();
System.out.println(digest);
md.reset();
md.update("string".getBytes());
byte[] digest2 = md.digest();
System.out.println(digest2);
You are outputting the byte[]
object, not the byte array contents. Use
System.out.println(Arrays.toString(digest1));
....
System.out.println(Arrays.toString(digest2));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With