Skip to content

Instantly share code, notes, and snippets.

@RevenueGitHubAdmin
Last active November 5, 2018 09:46
Show Gist options
  • Save RevenueGitHubAdmin/f6ab41870c10aea94affe65b10e7da99 to your computer and use it in GitHub Desktop.
Save RevenueGitHubAdmin/f6ab41870c10aea94affe65b10e7da99 to your computer and use it in GitHub Desktop.
[Java] How to calculate and encode an element digest.
/*
* The following snippet calculates the SHA512 digest hash of the received element and return it as a BASE64
* encode String. It is used to calculate the digest of the content of the request and it is an important
* element of the signature. The expected parameter is the plain text representation of the request's body
* being sent. For SOAP, it is the XML node being signed. For RESTful is the JSON document being sent.
*
* Gists provided for illustrative purposes only. Developers can use these as a support tool
* but the Office of the Revenue Commissioners (Revenue) does not provide any warranty with
* these gists.
*/
public static String calculateAndEncodeElementDigest(InputStream element) throws IOException, NoSuchAlgorithmException {
MessageDigest sha512Digest = MessageDigest.getInstance("SHA-512");
ByteArrayOutputStream elementOutputStream = new ByteArrayOutputStream();
byte[] elementData = new byte[1];
while(element.read(elementData) != -1) {
elementOutputStream.write(elementData);
}
elementOutputStream.flush();
byte[] digest = sha512Digest.digest(elementOutputStream.toByteArray());
return Base64.getEncoder().encodeToString(
digest
);
}
@neilm-fourjs
Copy link

This does NOT use sha512 on the input value!!! it is just returning the base64 encode version of the input string!!!!

@RevenueGitHubAdmin
Copy link
Author

Hi neilm-fourjs, apologies for the delayed response. We have updated this gist.

Thanks for your input. Please don't hesitate to highlight other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment