You should install OpenSSL and Libssl-dev before using the MD5 function.
Include header files: #include <openssl/md5.h>
Two different ways:
The first type:
int Md5_init (Md5_ctx *c);
int Md5_update (md5_ctx *c, const void *data, size_t len);
int md5_final (unsigned char *md, md5_ctx *c);
Three functions are used in conjunction for file verification.
The second type:
unsigned char *md5 (const unsigned char *d, unsigned long n,unsigned char *md);
Suitable for password encryption.
Cases
File check
#include <openssl/md5.h> #include <iostream> #include <string.h> #include <stdlib.h>using namespace Std;int Main () {FILE *fd=fopen ("Tmp.txt", "R"); Md5_ctx c;unsigned Char md[16];int len;char tmp[3]={' \ n '}, md5buf[33]={' \ n '};unsigned char buffer [1024]={' + '}; Md5_init (&c); while (0! = (len = fread (buffer, 1, 1024x768, FD))) { md5_update (&c, buffer, Len);} Md5_final (md,&c); for (int i = 0; i <; i++) {sprintf (tmp, "%02x", Md[i]); strcat (md5buf,tmp);} Cout<<md5buf<<endl;fclose (FD); return 0;}
Password encryption
#include <openssl/md5.h> #include <iostream> #include <string.h> #include <stdlib.h>using namespace Std;int Main () {string data = "HelloWorld"; unsigned char md[16]; int i; char tmp[3]={' \ n '}, md5buf[33]={' n '}; MD5 ((unsigned char *) data.c_str (), Data.length (), MD), for (int i=0; i<16; i++) {sprintf (tmp, "%02x", Md[i]); strcat (md5buf,tmp);} Cout<<md5buf<<endl;return 0;}
Compile-time specified library file:-lcrypto