Help Center/ SparkRTC/ FAQs/ Recording/ How Do I Authenticate a Recording Callback?
Updated on 2023-11-01 GMT+08:00

How Do I Authenticate a Recording Callback?

To ensure recording callback communication security, tenants can configure the recording callback parameter Private Key to authenticate recording callback messages. This section describes how to generate a recording callback authentication signature.

Parameter Description

Table 1 Parameter description

Parameter

Description

X-Rtc-Rand

Random number in the request header.

X-Rtc-Timestamp

Timestamp in the request header.

X-Rtc-Signature

Signature in the request header.

msg

Content of the request body.

Method of Generating a Recording Callback Authentication Signature

  1. Combine the following parameters into a string.

    The code is as follows:

    String content = X-Rtc-Rand + X-Rtc-Timestamp + msg;
  2. Use the callback key (key) in the recording callback configuration to encrypt the combined string content in HMAC-SHA256 mode to obtain the signed string.

    The code is as follows:

        String signatureStr = hmacSha(key, content, "HmacSHA256");
        static String hmacSha(String KEY, String VALUE, String SHA_TYPE) {
            try {
                SecretKeySpec signingKey = new SecretKeySpec(KEY.getBytes("UTF-8"), SHA_TYPE);
                Mac mac = Mac.getInstance(SHA_TYPE);
                mac.init(signingKey);
                byte[] rawHmac = mac.doFinal(VALUE.getBytes("UTF-8"));
    
                byte[] hexArray = {
                        (byte) '0', (byte) '1', (byte) '2', (byte) '3',
                        (byte) '4', (byte) '5', (byte) '6', (byte) '7',
                        (byte) '8', (byte) '9', (byte) 'a', (byte) 'b',
                        (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f'
                };
                byte[] hexChars = new byte[rawHmac.length * 2];
                for (int j = 0; j < rawHmac.length; j++) {
                    int v = rawHmac[j] & 0xFF;
                    hexChars[j * 2] = hexArray[v >>> 4];
                    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
                }
                return new String(hexChars);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }