MD5 Library of Linux

Source: Internet
Author: User
Tags install openssl sprintf

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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.