Reputation: 838
I am trying HMAC SHA256 hash function from Open SSL Library with C++, However when I print the result hex values I saw that my output differs everytime I run the code. What could be the problem
#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 <<" " ;
}
Upvotes: 1
Views: 506
Reputation: 31020
HMAC_SHA256 produces a SHA256 sum at the end, which is 32 bytes. EVP_MAX_MD_SIZE is 64 bytes, initialized with random memory garbage. If I compile and run your code, the first 32 bytes are always the same.
Upvotes: 1