Use the MD5 function under VC ++

Source: Internet
Author: User

Use Windows dynamic link library advapi32.dll.

This dynamic link library provides the following MD5 functions:

Md5init (& CTX );
Md5update (& CTX, Buf, Len );
Md5final (& CTX );

The usage is as follows:

Create File common. h

#include <windows.h>/* Data structure for MD5 (Message-Digest) computation */typedef struct { ULONG i[2];                          /* number of _bits_ handled mod 2^64 */ ULONG buf[4];                                           /* scratch buffer */ unsigned char in[64];                                     /* input buffer */ unsigned char digest[16];            /* actual digest after MD5Final call */} MD5_CTX;#define MD5DIGESTLEN 16#define PROTO_LIST(list)    list/** MTS: Each of these assumes MD5_CTX is locked against simultaneous use.*/typedef void (WINAPI* PMD5Init) PROTO_LIST ((MD5_CTX *));typedef void (WINAPI* PMD5Update) PROTO_LIST ((MD5_CTX *, const unsigned char *, unsigned int));typedef void (WINAPI* PMD5Final )PROTO_LIST ((MD5_CTX *));//end globle define  class Ccommon {public:  Ccommon(); virtual ~Ccommon(); const char * md5(const char * str); const char * Hex2ASC(const BYTE *Hex, int Len); PMD5Init MD5Init; PMD5Update MD5Update; PMD5Final MD5Final;};

Create File common. cpp

#include "stdafx.h"#include "common.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////Ccommon::Ccommon(){}Ccommon::~Ccommon(){}const char * Ccommon::Hex2ASC(const BYTE *Hex, int Len){static char  ASC[4096 * 2];int    i;for (i = 0; i < Len; i++){ASC[i * 2] = "0123456789abcdef"[Hex[i] >> 4];ASC[i * 2 + 1] = "0123456789abcdef"[Hex[i] & 0x0F];}ASC[i * 2] = 0;return ASC;}const char * Ccommon::md5(const char * str){MD5_CTX ctx;const unsigned char * buf = reinterpret_cast<const unsigned char *>(str);int len = strlen(str);HINSTANCE hDLL;if ( (hDLL = LoadLibraryA("advapi32.dll")) > 0 ){MD5Init = (PMD5Init)GetProcAddress(hDLL,"MD5Init");MD5Update = (PMD5Update)GetProcAddress(hDLL,"MD5Update");MD5Final = (PMD5Final)GetProcAddress(hDLL,"MD5Final");MD5Init(&ctx);MD5Update(&ctx, buf, len);MD5Final(&ctx);}return Hex2ASC(ctx.digest, 16);}

Call Method

// Introduce the header file # include "common. H" // declare the class cCommon md5class; name = md5class. MD5 (name );

Notes

The case sensitivity after MD5 encryption is indicated by the hex2asc function.

ASC[i * 2] = "0123456789abcdef"[Hex[i] >> 4];ASC[i * 2 + 1] = "0123456789abcdef"[Hex[i] & 0x0F];

Here I changed these two statements to lower case, which should be in upper case by default, depending on your needs.

If you want to change the value to uppercase:

ASC[i * 2] = "0123456789ABCDEF"[Hex[i] >> 4];ASC[i * 2 + 1] = "0123456789ABCDEF"[Hex[i] & 0x0F];

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.