Validating the Signature
To validate the signature, you can use this sample Java code or something similar. This
code includes the payload, signature received, and secret key.
You can have analogous code in your respective programming language.
1public static final String SIGNING_KEY_ALGO = "HMACSHA256";
2public static boolean isSignatureValid(String payload, String receivedSignature, String signingKey) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
3 Mac mac = Mac.getInstance(SIGNING_KEY_ALGO);
4 SecretKeySpec secretKeySpec =
5 new SecretKeySpec(
6 signingKey.getBytes(StandardCharsets.UTF_8), SIGNING_KEY_ALGO);
7 mac.init(secretKeySpec);
8
9 String signature =
10 org.apache.commons.codec.binary.Base64.encodeBase64String(mac.doFinal(payload.getBytes(StandardCharsets.UTF_8)));
11 if(signature.equals(receivedSignature))
12 {
13 return true;
14 }
15 return false;
16}
17