HMAC_Init在计算HMAC-SHA1时崩溃

时间:2011-12-20 04:44:37

标签: c++ openssl

这是我的代码:

int function(const char * buffer,size_t len,unsigned char * value)  
{   

char* user = "username";  
char*password = "password";  
size_t text_len = strlen(user) + strlen(password) + 2;  
  unsigned char* key = (unsigned char*)calloc(1,16);  
  unsigned char* text= (unsigned char *)calloc(1,text_len);  

  snprintf((char*)text, text_len, "%s:%s",user,password );  

MD5(text, text_len-1, key)

  HMAC_CTX *ctx = NULL;  
  unsigned int md_len = 20;  
  ctx = (HMAC_CTX*) calloc(1,sizeof(HMAC_CTX));  
  if(ctx == NULL){return -1;}

  HMAC_CTX_init(ctx);  
  `HMAC_Init(ctx, key, 16, EVP_sha1());`  //crashing everytime, saying heap corruption
  HMAC_Update(ctx, buffer, len);  
  HMAC_Final(ctx, value, &md_len);   

  HMAC_CTX_cleanup(ctx);  
return 0;  
}

我使用的是openssl 0.9.8.c.如果有人遇到这个问题,请告诉我。

1 个答案:

答案 0 :(得分:1)

根据man page HMAC_Init已弃用。可能值得尝试HMAC_Init_ex。