2026-05-05 22:10:26 +08:00

32 lines
855 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tencentrtc
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"strings"
)
const (
// CallbackHeaderSign 是腾讯 RTC 事件回调携带的签名 header。
CallbackHeaderSign = "Sign"
// CallbackHeaderSDKAppID 是腾讯 RTC 事件回调携带的应用 ID header。
CallbackHeaderSDKAppID = "SdkAppId"
)
// VerifyCallbackSignature 校验腾讯 RTC 事件回调签名。
func VerifyCallbackSignature(key string, body []byte, sign string) bool {
key = strings.TrimSpace(key)
sign = strings.TrimSpace(sign)
if key == "" || sign == "" {
// 回调入口必须 fail-closed没有 key 或签名都不能进入房间命令链路。
return false
}
mac := hmac.New(sha256.New, []byte(key))
_, _ = mac.Write(body)
expected := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return hmac.Equal([]byte(expected), []byte(sign))
}