无法获取md5哈希

用户名

我无法使用OpenSSL获得md5哈希。我正在使用以下命令进行构建:

gcc -Wall test_3.c -o test_3 -lcrypto -lssl

但是出现以下链接错误:

undefined reference to `MD5Init'
undefined reference to `MD5Update'
undefined reference to `MD5Final'
collect2: ld returned 1 exit status

该程序显示在下面:

#include<stdio.h>
#include<string.h>
#include <openssl/hmac.h>
#include <openssl/md5.h>

int main()
{
  char    digest[17];
  char input[] = "asdfljiahfbqhebfjcnajclgfeliuaef";
  int length = strlen(input);

  MD5_CTX md5;

  MD5Init(&md5);
  MD5Update(&md5,input, length);
  MD5Final(digest,&md5);
  printf("digest  is %s \n",digest);

  return 0;
}

如果您知道问题,请告诉我。请帮助我

hek2mgl

您犯了一些错误,我已纠正了这些错误。另外,我还添加了哈希的十六进制输出。否则会破坏您的终端。

#include <stdio.h>
#include <string.h>
#include <openssl/hmac.h>
#include <openssl/md5.h>

int main()
{
    // use unsigned char
    unsigned char    digest[16];
    char *input = "hek2mgl";
    int length = strlen(input);
    int i=0;

    // don't miss the underscore after MD5
    MD5_CTX md5;    
    MD5_Init(&md5);

    while (length > 0) {
        if (length > 512) {
            MD5_Update(&md5, input, 512);
        } else {
            MD5_Update(&md5, input, length);
        }
        length -= 512;
        input += 512;
    }

    MD5_Final(digest, &md5);
    printf("digest is: ");
    for(i = 0; i < 16; i++) {
        printf("%02x", digest[i]);
    }
    printf("\n");
    return 0;
}

输出:

digest is: 1ff8a3b2958ee3340ed88a2b980a8099

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章