实现效果:
py计算签名
计算签名TRTC文档说明:
文档地址:https://cloud.tencent.com/document/product/647/51586#.E8.AE.A1.E7.AE.97.E7.AD.BE.E5.90.8D
注意:这里的body内容,不要做任何转化,否则会计算签名错误!
回调密钥:
去trtc控制台配置回调密钥,地址:https://console.cloud.tencent.com/trtc
py代码逻辑:(python环境:python3.8)(注:此示例代码仅供参考!)
代码语言:txt复制# -*- coding: utf8 -*-
import hmac
import base64
from hashlib import sha256
# 功能:第三方回调sign校验
# 参数:
# key:控制台配置的密钥key
# body:第三方回调返回的body体
# sign:第三方回调返回的签名值sign
# 返回值:
# Status:OK 表示校验通过,FAIL 表示校验失败,具体原因参考Info
# Info:成功/失败信息
def checkSign(key, body, sign):
temp_dict = {}
computSign = base64.b64encode(hmac.new(key.encode('utf-8'), body.encode('utf-8'), digestmod=sha256).digest()).decode('utf-8')
print("生成的sign为:", computSign)
print("传入的sign为:", sign)
if computSign == sign:
temp_dict['Status'] = 'OK'
temp_dict['Info'] = '校验通过'
return temp_dict
else:
temp_dict['Status'] = 'FAIL'
temp_dict['Info'] = '校验失败'
return temp_dict
if __name__ == '__main__':
key = 'testKey'
message ='{nt"EventGroupId":t1,nt"EventType":t104,nt"CallbackTs":t1634025594562,nt"EventInfo":t{ntt"RoomId":t"696969",ntt"EventTs":t1634025594,ntt"EventMsTs":t1634025594520,ntt"UserId":t"test10",ntt"UniqueId":t1634025590938,ntt"Role":t20,ntt"Reason":t1nt}n}'
sign = 'ykT 66vvx Q9tyAEAJIyp5D/zOyPlaSNlsU2MB7InuU='
result = checkSign(key, message, sign)
print(result)
注意:此示例代码仅供参考!