Rtmp: 优化复杂握手c0c1生成算法性能

This commit is contained in:
ziyue 2021-12-22 22:20:28 +08:00
parent ab14585a81
commit 4e01c29833

View File

@ -433,17 +433,19 @@ static u_int8_t FPKey[] = {
//发送复杂握手c0c1 //发送复杂握手c0c1
void RtmpHandshake::create_complex_c0c1() { void RtmpHandshake::create_complex_c0c1() {
memcpy(zero, "\x80\x00\x07\x02", 4); memcpy(zero, "\x80\x00\x07\x02", 4);
string str((char *) this, sizeof(*this)); //digest随机偏移长度
auto offset_value = rand() % (C1_SCHEMA_SIZE - C1_OFFSET_SIZE - C1_DIGEST_SIZE); auto offset_value = rand() % (C1_SCHEMA_SIZE - C1_OFFSET_SIZE - C1_DIGEST_SIZE);
str.erase(8 + C1_SCHEMA_SIZE + C1_OFFSET_SIZE + offset_value, C1_DIGEST_SIZE); //设置digest偏移长度
auto offset_ptr = random + C1_SCHEMA_SIZE;
auto offset_ptr = (uint8_t *) str.data() + 8 + C1_SCHEMA_SIZE;
offset_ptr[0] = offset_ptr[1] = offset_ptr[2] = offset_value / 4; offset_ptr[0] = offset_ptr[1] = offset_ptr[2] = offset_value / 4;
offset_ptr[3] = offset_value - 3 * (offset_value / 4); offset_ptr[3] = offset_value - 3 * (offset_value / 4);
//去除digest后的剩余随机数据
string str((char *) this, sizeof(*this));
str.erase(8 + C1_SCHEMA_SIZE + C1_OFFSET_SIZE + offset_value, C1_DIGEST_SIZE);
//获取摘要
auto digest_value = openssl_HMACsha256(FPKey, C1_FPKEY_SIZE, str.data(), str.size()); auto digest_value = openssl_HMACsha256(FPKey, C1_FPKEY_SIZE, str.data(), str.size());
str.insert(8 + C1_SCHEMA_SIZE + C1_OFFSET_SIZE + offset_value, digest_value); //插入摘要
memcpy(this, str.data(), sizeof(*this)); memcpy(random + C1_SCHEMA_SIZE + C1_OFFSET_SIZE + offset_value, digest_value.data(), digest_value.size());
} }
void RtmpProtocol::check_C1_Digest(const string &digest,const string &data){ void RtmpProtocol::check_C1_Digest(const string &digest,const string &data){