Newer Version Available

This content describes an older version of this product. View Latest

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
1import hmac
2import hashlib
3import base64
4
5def is_signature_valid(payload, received_signature, signing_key):
6    mac = hmac.new(signing_key.encode('latin-1'), digestmod=hashlib.sha256)
7    mac.update(payload.encode('latin-1'))
8    signature = base64.b64encode(mac.digest()).decode('utf-8')
9
10    return signature == received_signature
11