javadata-integritymessage-digest

When I hash string using sha-256 and compare it with online hash tools the results are different using java 8?


When I trying to hash string in java 8 using SHA-256 with different library such as MessageDigest, guava and apache the result is not same in the online tool here what I do:

public static String hash(String planText){
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    byte[] hashed = messageDigest.digest(planText.getBytes(StandardCharsets.UTF_8));
    String encodeHash = Base64.getEncoder().encodeToString(hashed);
    return encodeHash;
 }

for this String:

<root>
   <test>
      abc
   </test>
</root>

the return of this function is lPJDgg2R+x+/H6qHZ59lACAemdF7/JFeQex+otuQ7Xk=. for the online tool is c8awdfivV+DgU43KR5uK8xEh5ginLGATnJdyfs1573w= Hash generator tool.

the benefit of using hashing to achieve data integrity, so there something I miss to do?


Solution

  • A better way to address this would be to parse string line by line, then remove indents, whitespaces and also trim(if required).

    I got consistent output with this enter image description here

    enter image description here