openssl是开源的,可以在上面编写测试代码,怎样编写呢?在此举一个例子。
第一步:编写1个测试程序MD5test1.c,代码如下
#include<stdio.h>#include<openssl/md5.h>#include<string.h>int main(int argc,char**argv ){MD5_CTX ctx;unsignedchar*data="123";unsignedchar md[16];char buf[33]={'\0'};char tmp[3]={'\0'};int i;MD5_Init(&ctx);MD5_Update(&ctx,data, strlen (data));MD5_Final(md,&ctx);for( i=0; i<16; i++){sprintf (tmp,"%02X",md[i]);strcat (buf,tmp);}printf ("%s\n",buf);return0;}
第二步:将该代码放在如下目录:
/openssl-1.1.0c/test
第三步:编译MD5test1.c。
[root@localhost test]# gcc -o MD5test1 MD5test1.c
/tmp/cczP8S6Z.o: In function `main':
MD5test1.c:(.text+0x6b): undefined reference to `MD5_Init'
MD5test1.c:(.text+0x8d): undefined reference to `MD5_Update'
MD5test1.c:(.text+0xa0): undefined reference to `MD5_Final'
collect2: error: ld returned 1 exit status
出现了三个错误。
错误原因是
包含md5函数的库为/usr/lib/libcrypto.a(.so),编译时要使用-lcrypto。
第四步:重新编译MD5test1.c。
[root@localhost test]# gcc -o MD5test1 MD5test1.c -lcrypto
编译成功。
第五步:运行MD5test1
[root@localhost test]# ./MD5test1
202CB962AC59075B964B07152D234B70
本文介绍如何使用OpenSSL库编写一个简单的MD5测试程序,并提供了从编写到编译运行的完整步骤。通过一个具体示例展示了如何正确链接库以避免未定义引用错误。
848

被折叠的 条评论
为什么被折叠?



