我必须为给定的输入生成256位长度的哈希值。我正在使用'EVP_sha256'。
错误:未在此范围内声明'EVP_sha256'
以下是我的代码---
HMAC_Init_ex(&hmac, key, keyLength, EVP_sha256(), NULL);
我在我的cpp文件中包含了openssl / evp.h。
使用openssl版本 OpenSSL 1.0.1e-fips 2013年2月11日。
任何指针?
以下计划
文件名:Generatehash.cpp
#include <iostream>
#include <string.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <sstream>
#include <iomanip>
#include <bitset>
using namespace std;
int KEY_SIZE = 256;
int AES_BLOCK_SIZE = 16;
int IV_SIZE = 16;
void hmac(unsigned char* key, int keyLength, string msg, unsigned char* hash)
{
HMAC_CTX hmac;
HMAC_CTX_init(&hmac);
HMAC_Init_ex(&hmac, key, keyLength, EVP_sha256(), NULL);
if(HMAC_Update(&hmac, ( unsigned char* )&msg[0], msg.length()))
cout<<"Update success"<<endl;
unsigned int len = 0;
if(HMAC_Final(&hmac, hash, &len))
cout<<"Final success"<<endl;
HMAC_CTX_cleanup(&hmac);
}
int GenerateHashValue(unsigned char* encryptionKey, int keyLength, string uri, unsigned char* iv)
{
unsigned char hashBuffer[32] = {'\0'};
hmac(encryptionKey, keyLength, uri, hashBuffer);
}
int main()
{
std::cout<<"TEST START =====>>\n";
string key = "Jwe9#z+IND.niSNDJwe9#z+IND.niSND";
string strData = "www.format@imminent.com";
unsigned char iv[16] = {'\0'};
GenerateHashValue((unsigned char*)key.c_str(),key.length(),strData, iv);
std::cout<<"TEST END =====>>\n";
return 0;
}
用于编译的命令
g ++ Generatehash.cpp -lssl -lcrypto