HashUtils { public static String getMessageHash(JSONObject message, Collection<String> hashFields) { List<String> hashElements = hashFields.stream().map(errorField -> String.format("%s-%s", errorField, message.get(errorField))).collect(Collectors.toList()); return DigestUtils.sha256Hex(String.join("|", hashElements).getBytes(UTF_8)); }  static String getMessageHash(JSONObject message, Collection<String> hashFields); static String getMessageHash(JSONObject message); static String getMessageHash(byte[] bytes);  }
@Test public void getMessageHashShouldReturnHashForHashFields() { JSONObject message = new JSONObject(); message.put("field1", "value1"); message.put("field2", "value2"); message.put("field3", "value3"); Collection<String> fields = Arrays.asList("field2", "field3"); assertEquals("6eab1c2c827387803ce457c76552f0511858fc1f9505c7dc620e198c0d1f4d02", HashUtils.getMessageHash(message, fields)); }
@Test public void getMessageHashShouldReturnHashForMessage() { JSONObject message = new JSONObject(); message.put("field1", "value1"); message.put("field2", "value2"); message.put("field3", "value3"); assertEquals("a76cdafc5aa49180c0b22c78d4415c505f9997c54847cec6c623f4cacf6a2811", HashUtils.getMessageHash(message)); }
@Test public void getMessageHashShouldReturnHashForBytes() { assertEquals("ab530a13e45914982b79f9b7e3fba994cfd1f3fb22f71cea1afbf02b460c6d1d", HashUtils.getMessageHash("message".getBytes(UTF_8))); }