linux - 无法获取 md5 哈希

标签 linux ssl openssl 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;
}

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

最佳答案

你犯了一些错误,我已经改正了。我还添加了哈希的十六进制输出。否则它会破坏你的终端。

#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

关于linux - 无法获取 md5 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756900/

相关文章:

openssl - 如何在两种公钥格式之间进行转换,一种是 "BEGIN RSA PUBLIC KEY",另一种是 "BEGIN PUBLIC KEY"

linux - 终端错误 : cp: illegal option

php - openssl 在 php 和 c 上获得不同的 DH 公钥

ssl - 创建供本地使用的 EV SSL 证书

ssl - LittleProxy 和公司证书

java - 使用 IBM Java 通过 SSL 连接到 Oracle DB

java - CAS SunCertPathBuilderException :unable to find valid certification path to requested target

linux - Linux 中的 TCP 套接字数据不一致

linux - 使用 ansible 启动配置模块 ec2_lc 和安全组名称与 id

c - 如何使用c程序在多个终端窗口中输出