Why does MessageDigest return different responses for the same line?

I am trying to get the hash value for a string using MessageDigest in Java, but the value changes every time. When I run the program twice, it will again have 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); 
+6
source share
1 answer

You output the byte[] object, not the contents of the byte array. Use

 System.out.println(Arrays.toString(digest1)); .... System.out.println(Arrays.toString(digest2)); 
+10
source

Source: https://habr.com/ru/post/909105/


All Articles