The C language version that is exactly the same as the PHP MD5 Algorithm

Source: Internet
Author: User
Tags md5 digest syslog

I have seen many people search for the MD5 algorithm. I specifically wrote a C language version based on the MD5 Algorithm of PHP. If you are interested,

You can use this algorithm to generate an MD5 Digest, which is identical to the MD5 function of PHP.

/**
* MD5 functions, compatible with PHP
* @ Author wangzebin

*/

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <syslog. h>

/*
* Convert an array of little-Endian words to a hex string.
*/
Int binl2hex (int * SRC, char * DST, int nsize)
{
If (src = 0)
Return 1;

If (DST = 0)
Return 1;

If (nsize <32)
Return 1;

Char hex_tab [] = "0123456789 abcdef ";

Int J = 0;
For (INT I = 0; I <16; I ++)
{
DST [J ++] = hex_tab [(SRC [I> 2]> (I % 4) * 8 + 4) & 0xf];

Syslog (log_debug, "end: % C-% d", DST [J-1], (SRC [I> 2]> (I % 4) * 8 + 4), (I % 4) * 8 + 4 ), SRC [I> 2]);

DST [J ++] = hex_tab [(SRC [I> 2]> (I % 4) * 8) & 0xf];
Syslog (log_debug, "end: % C-% d", DST [J-1], (SRC [I> 2]> (I % 4) * 8 + 4), (I % 4) * 8 + 4 ), SRC [I> 2]);
}
DST [J] = 0;
Return 0;
}

/*
* Convert a string to an array of little-Endian words
* If chrsz is ASCII, characters> 255 have their hi-byte silently ignored.
*/
Int str2binl (char * SRC, int * pndst, int nsize)
{
If (src = 0)
Return 1;

If (pndst = 0)
Return 1;

Int nloop = strlen (SRC) * 8;
If (nloop> 5)> = nsize)
Return 1;

For (INT I = 0; I <nsize; I ++)
Pndst [I] = 0;

Int mask = (1 <8)-1;

For (INT I = 0; I <nloop; I + = 8)
{
Pndst [I> 5] | = (SRC [I/8] & Mask) <(I % 32 );
}

Return 0;
}

/*
* Add integers, wrapping at 2 ^ 32.
*/
Int safe_add (int x, int y)
{
Int Lsw = (X & 0 xFFFF) + (Y & 0 xFFFF );
Int MSW = (x> 16) + (Y> 16) + (Lsw> 16 );
Return (MSW <16) | (Lsw & 0 xFFFF );
}

/*
* Bitwise rotate a 32-bit number to the left.
*/
Int bit_rol (unsigned int num, unsigned int CNT)
{
// Old
// Return (Num <CNT) | (Num >>> (32-CNT ));
Return (Num <CNT) | (Num> (32-CNT ));
}

/*
* These functions implement the four basic operations the algorithm uses.
*/
Int md5_cen (int q, int A, int B, int X, int S, int T)
{
Return safe_add (bit_rol (safe_add (A, q), safe_add (x, t), S), B );
}
Int md5_ff (int A, int B, int C, int D, int X, int S, int T)
{
Return md5_cen (B & C) | ((~ B) & D), a, B, X, S, T );
}
Int md5_gg (int A, int B, int C, int D, int X, int S, int T)
{
Return md5_cen (B & D) | (C &(~ D), a, B, X, S, T );
}
Int md5_hh (int A, int B, int C, int D, int X, int S, int T)
{
Return md5_cen (B ^ C ^ D, a, B, X, S, T );
}
Int md5_ii (int A, int B, int C, int D, int X, int S, int T)
{
Return md5_cen (C ^ (B | (~ D), a, B, X, S, T );
}

/*
* Calculate the MD5 of an array of little-Endian words, and a bit length
*/
Int core_md5 (int * X, int Len, int * DST)
{
If (x = 0)
Return 1;

If (LEN <= 0)
Return 1;

If (DST = 0)
Return 1;

/* Append padding */
X [Len> 5] | = 0x80 <(LEN) % 32 );

Syslog (log_debug, "X [Len> 5]: % d", X [Len> 5]);
// Old
// -- X [(LEN + 64) >>> 9) <4) + 14] = Len;

Unsigned int ntemp = Len + 64;
X [(ntemp)> 9) <4) + 14] = Len;
Syslog (log_debug, "X [(ntemp)> 9) <4) + 14]: % d", X [(ntemp)> 9) <4) + 14]);

Int A = 1732584193;
Int B =-271733879;
Intc =-1732584194;
Int d = 271733878;

Int ncurrlen = (ntemp)> 9) <4) + 14 + 1;
For (INT I = 0; I <ncurrlen; I + = 16)
{
Int olda =;
Int oldb = B;
Int oldc = C;
Int oldd = D;

A = md5_ff (a, B, c, d, X [I + 0], 7,-680876936 );
D = md5_ff (D, a, B, c, X [I + 1], 12,-389564586 );
C = md5_ff (c, d, A, B, X [I + 2], 17,606 105819 );
B = md5_ff (B, c, d, A, X [I + 3], 22,-1044525330 );

A = md5_ff (a, B, c, d, X [I + 4], 7,-176418897 );
D = md5_ff (D, a, B, c, X [I + 5], 12,120 0080426 );
C = md5_ff (c, d, A, B, X [I + 6], 17,- 1473231341 );
B = md5_ff (B, c, d, A, X [I + 7], 22,-45705983 );

A = md5_ff (a, B, c, d, X [I + 8], 7, 1770035416 );
D = md5_ff (D, a, B, c, X [I + 9], 12,-1958414417 );
C = md5_ff (c, d, A, B, X [I + 10], 17,- 42063 );
B = md5_ff (B, c, d, A, X [I + 11], 22,-1990404162 );

A = md5_ff (a, B, c, d, X [I + 12], 7, 1804603682 );
D = md5_ff (D, a, B, c, X [I + 13], 12,-40341101 );
C = md5_ff (c, d, A, B, X [I + 14], 17,- 1502002290 );
B = md5_ff (B, c, d, A, X [I + 15], 22,123 6521329 );

A = md5_gg (a, B, c, d, X [I + 1], 5,-165796510 );
D = md5_gg (D, a, B, c, X [I + 6], 9,-1069501632 );
C = md5_gg (c, d, A, B, X [I + 11], 14,643 717713 );
B = md5_gg (B, c, d, A, X [I + 0], 20,-373897302 );

A = md5_gg (a, B, c, d, X [I + 5], 5,-701558691 );
D = md5_gg (D, a, B, c, X [I + 10], 9, 38016083 );
C = md5_gg (c, d, A, B, X [I + 15], 14,-660478335 );
B = md5_gg (B, c, d, A, X [I + 4], 20,-405537848 );

A = md5_gg (a, B, c, d, X [I + 9], 5, 568446438 );
D = md5_gg (D, a, B, c, X [I + 14], 9,-1019803690 );
C = md5_gg (c, d, A, B, X [I + 3], 14,-187363961 );
B = md5_gg (B, c, d, A, X [I + 8], 20,116 limit 1501 );

A = md5_gg (a, B, c, d, X [I + 13], 5,-1444681467 );
D = md5_gg (D, a, B, c, X [I + 2], 9,-51403784 );
C = md5_gg (c, d, A, B, X [I + 7], 14,173 5328473 );
B = md5_gg (B, c, d, A, X [I + 12], 20,-1926607734 );

A = md5_hh (a, B, c, d, X [I + 5], 4,-378558 );
D = md5_hh (D, a, B, c, X [I + 8], 11,-2022574463 );
C = md5_hh (c, d, A, B, X [I + 11], 16,183 9030133 );
B = md5_hh (B, c, d, A, X [I + 14], 23,-35309556 );

A = md5_hh (a, B, c, d, X [I + 1], 4,-1530992060 );
D = md5_hh (D, a, B, c, X [I + 4], 11,127 2893353 );
C = md5_hh (c, d, A, B, X [I + 7], 16,-155497632 );
B = md5_hh (B, c, d, A, X [I + 10], 23,-1094730640 );

A = md5_hh (a, B, c, d, X [I + 13], 4, 681279174 );
D = md5_hh (D, a, B, c, X [I + 0], 11,-358537222 );
C = md5_hh (c, d, A, B, X [I + 3], 16,-722521979 );
B = md5_hh (B, c, d, A, X [I + 6], 23,760 29189 );

A = md5_hh (a, B, c, d, X [I + 9], 4,-640364487 );
D = md5_hh (D, a, B, c, X [I + 12], 11,-421815835 );
C = md5_hh (c, d, A, B, X [I + 15], 16,530 742520 );
B = md5_hh (B, c, d, A, X [I + 2], 23,-995338651 );

A = md5_ii (a, B, c, d, X [I + 0], 6,-198630844 );
D = md5_ii (D, a, B, c, X [I + 7], 10,112 6891415 );
C = md5_ii (c, d, A, B, X [I + 14], 15,-1416354905 );
B = md5_ii (B, c, d, A, X [I + 5], 21,-57434055 );

A = md5_ii (a, B, c, d, X [I + 12], 6, 1700485571 );
D = md5_ii (D, a, B, c, X [I + 3], 10,-1894986606 );
C = md5_ii (c, d, A, B, X [I + 10], 15,-1051523 );
B = md5_ii (B, c, d, A, X [I + 1], 21,-2054922799 );

A = md5_ii (a, B, c, d, X [I + 8], 6, 1873313359 );
D = md5_ii (D, a, B, c, X [I + 15], 10,-30611744 );
C = md5_ii (c, d, A, B, X [I + 6], 15,-1560198380 );
B = md5_ii (B, c, d, A, X [I + 13], 21,130 9151649 );

A = md5_ii (a, B, c, d, X [I + 4], 6,-145523070 );
D = md5_ii (D, a, B, c, X [I + 11], 10,-1120210379 );
C = md5_ii (c, d, A, B, X [I + 2], 15,718 787259 );
B = md5_ii (B, c, d, A, X [I + 9], 21,-343485551 );

A = safe_add (A, olda );
B = safe_add (B, oldb );
C = safe_add (C, oldc );
D = safe_add (D, oldd );
}

DST [0] =;
DST [1] = B;
DST [2] = C;
DST [3] = D;

Return 0;

}

/*
* This is the function you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
Int hex_md5 (char * SRC, char * DST, int nsize)
{
If (DST = 0)
Return 1;

If (nsize <32)
Return 1;

Int nbuffsize1 = (strlen (SRC) * 8)> 5) + 1;
Int nbuffsize2 = (strlen (SRC) * 8 + 64)> 9) <4) + 15;

Int nbuffsize = nbuffsize1> nbuffsize2? Nbuffsize1: nbuffsize2;

Int * buff = new int [nbuffsize];
If (buff = 0)
Return 1;

Int ptemp [4];
For (INT I = 0; I <4; I ++)
{
* Ptemp = 0;
}

If (str2binl (SRC, buff, nbuffsize) = 1)
{
Delete Buff;
Return 1;
}

If (core_md5 (buff, strlen (SRC) * 8, ptemp) = 1)
{
Delete Buff;
Return 1;
}

Delete Buff;

If (binl2hex (ptemp, DST, 32) = 1)
{
Syslog (log_debug, "error ");
Return 1;
}

Return 0;
}

// For example: input a string and get a MD5 string.
/*
Int main ()
{
Char SRC [2, 1024];
Char MD5 [32] = {0 };

Strcpy (SRC, "1234 ");

Hex_md5 (SRC, MD5, 32 );

Printf ("/nmd5: % s/n", MD5 );

Return 0;
}*/

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.