This document draws on the files on the Internet and makes some modifications on its own. It mainly solves some problems caused by inconsistencies and not details during the test.
OpenSSL Installation
Centos Yum install OpenSSL-devel
# Include <stdio. h> # include <string. h> # include <stdlib. h ># include <OpenSSL/md5.h> int main (INT argc, char ** argv) {md5_ctx hash_ctx; char input_string [128]; unsigned char hash_ret [16]; int I; // check usage if (argc! = 2) {fprintf (stderr, "% S <input string> \ n", argv [0]); exit (-1) ;}// set the input string, here is a newline file. If a newline is added to the variable, the value of snprintf (input_string, sizeof (input_string), "% s", argv [1]) is incorrect. // initialize a hash context md5_init (& hash_ctx); // update the input string to the hash context (you can update // more string to the hash context) md5_update (& hash_ctx, input_string, strlen (input_string); // compute the hash result md5_final (hash_ret, & hash_ctx); // print printf ("input string: % s", input_string ); printf ("output string:"); for (I = 0; I <32; ++ I) {if (I % 2 = 0) {printf ("% x", (hash_ret [I/2]> 4) & 0xf);} else {printf ("% x ", (hash_ret [I/2]) & 0xf) ;}} printf ("\ n"); Return 0 ;}
Compile and check. You need to link the OpenSSL library.
Gcc-lcrypto main2.c-O main2
Test Data
./Main2 1234
Input string: 1234 output string: 81dc9bdb52d04dc20036dbd8313ed055
Check whether the output is correct. Find the correct tool pair and run md5sum in Linux.
Echo-n" 1234 "| md5sum-
Echo-N is to remove the line feed. If not