Skip to content

Instantly share code, notes, and snippets.

@mrmuscle1234
Created November 23, 2021 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmuscle1234/20c9d46d163fee66528449c0ea8419a7 to your computer and use it in GitHub Desktop.
Save mrmuscle1234/20c9d46d163fee66528449c0ea8419a7 to your computer and use it in GitHub Desktop.
HMAC signature
// base string - this string will be used to generate mac signature.
StringBuilder baseString = new StringBuilder();
/*
* The order is CRITICAL! And no "\n" at the end.
*
* Timestamp + \n + nonce + \n+ httpmethod + \n +
* host + \n + path + \n + bodyHash
*/
String newline = "\n";
baseString.append(ts).append(newline).append(nonce).append(newline)
.append(host).append(newline).append(resourcePath).append(newline)
.append(bodyHash);
// Generate signature using client secret (crypto initialized above)
byte[] signatureBytes = mac.doFinal(baseString.toString().getBytes(UTF8_ENCODING));
// now encode the cypher for the web
String signatureStr = Base64.encodeBase64String(signatureBytes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment