The first program of openssl under Linux

OpenSSL is open source, you can write test code on it, how to write it? Here is an example of this.

The first step: write a test program MD5test1.c, the code is as follows
  1. #include<stdio.h>
  2. #include<openssl/md5.h>
  3. #include<string.h>
  4. int main(int argc,char**argv )
  5. {
  6. MD5_CTX ctx;
  7. unsignedchar*data="123";
  8. unsignedchar md[16];
  9. char buf[33]={'\0'};
  10. char tmp[3]={'\0'};
  11. int i;
  12. MD5_Init(&ctx);
  13. MD5_Update(&ctx,data, strlen (data));
  14. MD5_Final(md,&ctx);
  15. for( i=0; i<16; i++)
  16. {
  17. sprintf (tmp,"%02X",md[i]);
  18. strcat (buf,tmp);
  19. }
  20. printf ("%s\n",buf);
  21. return0;
  22. }
Step 2: Put the code in the following directory:
/openssl-1.1.0c/test
Step 3: Compile 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
Three errors occurred.
The reason for the error is
The library containing the md5 function is /usr/lib/libcrypto.a(.so), use -lcrypto when compiling.
Step 4: Recompile MD5test1.c.
[root@localhost test]# gcc -o MD5test1 MD5test1.c  -lcrypto
Compilation succeeded.
Step 5: Run MD5test1
[root@localhost test]# ./MD5test1 
202CB962AC59075B964B07152D234B70

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326943675&siteId=291194637