C++ OPEN SSL 库 HMAC 功能返回值每次运行时都不相同

C++ OPEN SSL Library HMAC funtion return value is not same everytime I run?

本文关键字:运行时 返回值 SSL OPEN HMAC 功能 C++      更新时间:2023-10-16

我正在尝试使用 C++ 打开 SSL 库中的 HMAC SHA256 哈希函数,但是当我打印结果十六进制值时,我看到每次运行代码时我的输出都不同。可能是什么问题

#include <iostream>
#include <string>
#include <openssl/hmac.h>
int main(){
std::cout <<  "Generating key for RRC" << std::endl;
std::array <char, 32> test = {0x69, 0x01, 0x01,  0x00, 0x01, 0x01, 0x01,  0x00, 0x01, 0x01,
                             0x00, 0x01, 0x01, 0x01,  0x00, 0x01, 0x01, 0x01,  0x00, 0x01,
                             0x00, 0x01, 0x01, 0x01,  0x00, 0x01, 0x01, 0x01,  0x00, 0x01,
                             0x00, 0x01 };
std::array <char, 5> string = = {0x69, 0x03, 0x01,  0x02, 0x01 };
unsigned int lengthResult;
unsigned char result[EVP_MAX_MD_SIZE];
HMAC(EVP_sha256(), (unsigned char*)test.data(), test.size(),
                            (unsigned char*)string.data(), string.size(),
                             result, &lengthResult);
for (auto i:result)
    std::cout  << i + 0 <<" " ;
}

HMAC_SHA256末尾生成一个 SHA256 总和,即 32 个字节。EVP_MAX_MD_SIZE是 64 字节,用随机内存垃圾初始化。如果我编译并运行您的代码,前 32 个字节总是相同的。