MD5 HMAC与OpenSSL的 [英] MD5 HMAC With OpenSSL

查看:844
本文介绍了MD5 HMAC与OpenSSL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成MD5 HMAC与OpenSSL的&安培;大部分的code是借来的。该HMAC是产生不正确:

I was trying to generate MD5 HMAC with OpenSSL & most of the code is borrowed. The hmac being generate is incorrect:

#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <syslog.h>
#include <string.h>

#include <openssl/engine.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() 
{
  unsigned char* key = (unsigned char*) "2012121220121212201212122012121220121212201212122012121220121212";
  unsigned char* data = (unsigned char*) "johndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoejohndoe";
  unsigned char* expected = (unsigned char*) "abcd1d87dca34f334786307d0da4fcbd";
  unsigned char* result;
  // unsigned int result_len = 16;
  unsigned int result_len = 16;
  int i;
  static char res_hexstring[32];

  // result = HMAC(EVP_sha256(), key, 4, data, 28, NULL, NULL);
  result = HMAC(EVP_md5(), key, 32, data, 28, NULL, NULL);
  for (i = 0; i < result_len; i++) {
    sprintf(&(res_hexstring[i * 2]), "%02x", result[i]);
  }

  if (strcmp((char*) res_hexstring, (char*) expected) == 0) {
    printf("Test ok, result length %d\n", result_len);
  } else {
    printf("Got %s instead of %s\n", res_hexstring, expected);
  }
}

正在产生的散列是不正确。我想AP preciate一些反馈或有人指着我在正确的方向。

The hash being produced is incorrect. I would appreciate some feedback or someone pointing me in the right direction.

推荐答案

第三个和第五个参数为HMAC是错误的。你必须通过密钥的长度和数据的长度。在你的榜样,这是分别为64和84,而不是32和28。

The third and fifth parameters to HMAC are just wrong. You have to pass the length of the key and the length of data. In your example, this is respectively 64 and 84, not 32 and 28.

所以:

-    result = HMAC(EVP_md5(), key, 32, data, 28, NULL, NULL);                                                                                                                                                                         
+    result = HMAC(EVP_md5(), key, 64, data, 84, NULL, NULL); 

通过这种修改,它似乎是工作的罚款。

With this modification, it seems to be working fine.

这篇关于MD5 HMAC与OpenSSL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆