使用 Open SSL 运行 C 代码

发布于 2024-11-01 06:20:17 字数 1181 浏览 1 评论 0原文

我无法编译以下代码。我在 MACOSX 中使用终端中的以下命令运行它:
$gcc filename.c -lssl

我知道 MACOS 已经有一个内置的 openssl (我可以在终端中作为命令调用)。但我不确定我是否以正确的方式将其链接到库。

#include <stdio.h>
#include <openssl/evp.h>
main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;

OpenSSL_add_all_digests();

if(!argv[1]) {
    printf("Usage: mdtest digestname\n");
    exit(1);
}

md = EVP_get_digestbyname(argv[1]);

if(!md) {
    printf("Unknown message digest %s\n", argv[1]);
    exit(1);
}

EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);

printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}

我的错误是:
未定义的符号: “_EVP_MD_CTX_cleanup”,引用自:ccfZG7WJ.o中的_main
ld:未找到符号 collect2: ld returned 1 exit status

我对这个错误感到惊讶,因为它显示“未定义的符号”而不是“找不到头文件”。你能帮我调试这个问题吗?

谢谢!

I am not able to compile the following code. I run it in a MACOSX with the following command in the terminal:
$gcc filename.c -lssl

I understand that MACOS already has an inbuilt openssl (which i can call as a command in the terminal). But I am not sure if i am linking it to the library in the correct fashion.

#include <stdio.h>
#include <openssl/evp.h>
main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;

OpenSSL_add_all_digests();

if(!argv[1]) {
    printf("Usage: mdtest digestname\n");
    exit(1);
}

md = EVP_get_digestbyname(argv[1]);

if(!md) {
    printf("Unknown message digest %s\n", argv[1]);
    exit(1);
}

EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);

printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}

My error is:
Undefined symbols:
"_EVP_MD_CTX_cleanup", referenced from: _main in ccfZG7WJ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I am surprised by the error since it shows "Undefined Symbols" instead of "Header file not found". Can you please help me debug this issue.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够运 2024-11-08 06:20:17

OpenSSL 有 2 个库:libssllibcrypto。也包括 libcrypto。

gcc filename.c -lssl -lcrypto

(实际上,这个程序根本不需要 -lssl ,但包含它也没什么坏处)

libssl 用于建立实际的 SSL 或 TLS 连接,例如使用 https 连接到 Web 服务器时。它还导出 SSL_load_error_strings,即使仅使用加密库,这也很有用。

libcrypto 是密码学库。它具有加密算法、密钥和证书生成与验证功能、消息认证算法等。

OpenSSL has 2 libraries: libssl and libcrypto. Include libcrypto, too.

gcc filename.c -lssl -lcrypto

(You actually don't need -lssl at all for this program, but it doesn't hurt to include it)

libssl is for making actual SSL or TLS connections, like when connecting to a web server with https. It also exports SSL_load_error_strings, which is useful even when working only with the cryptography library.

libcrypto is the cryptography library. It has encryption algorithms, key and certificate generation and verification functions, message authentication algorithms, and so on.

夜空下最亮的亮点 2024-11-08 06:20:17

您怀疑该库未正确链接是正确的。有两种可能性:

  • 该库根本没有被链接,
  • 正在链接的库不包含该符号,表明版本不匹配。

第二个似乎更有可能,但第一个更容易检查。 man -k ssl 应该指出库手册页。

You're right to suspect the library isn't being linked correctly. There are two possibilities:

  • the library isn't being linked at all
  • the library being linked doesn't include that symbol, suggesting a version mismatch.

The second one seems more probable, but the first is easier to check. A man -k ssl should point out the library man pages.

东北女汉子 2024-11-08 06:20:17

在您的情况下,“未定义的符号”很可能意味着您没有链接正确的库。

Most probably in your case "Undefined Symbols" means that you are not linking the correct library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文