Skip to content

Instantly share code, notes, and snippets.

@RevenueGitHubAdmin
Created June 14, 2018 16:11
Show Gist options
  • Save RevenueGitHubAdmin/22566bb275f5b084d78e3532c0947d3c to your computer and use it in GitHub Desktop.
Save RevenueGitHubAdmin/22566bb275f5b084d78e3532c0947d3c to your computer and use it in GitHub Desktop.
[JavaScript] How to calculate and encode an element digest.
/*
* The following snippet uses the Forge JavaScript library (https://github.com/digitalbazaar/forge)
* to calculate 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 beingin sent. For more information about how to use the
* Forge JavaScript library, please, refer to the aforementioned URL.
*
* 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.
*/
function calculateAndEncodeElementDigest(element) {
return forge.util.encode64(
forge.md.sha512
.create()
.update(element)
.digest()
.getBytes()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment