How to use c to obtain the MD5 value of a file

Source: Internet
Author: User

There are md5.c and md5.h online, but there are only MD5Init (), MD5Update (), and MD5Final () functions,
Only the characters can be operated directly, but there is no interface for directly obtaining file md5. The following is my implementation, which can calculate the md5 value of 32-bit and 16-bit. Copy codeThe Code is as follows: # include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include "md5.h"
Char * MD5_file (char * path, int md5_len)
{
FILE * fp = fopen (path, "rb ");
MD5_CTX mdContext;
Int bytes;
Unsigned char data [1024];
Char * file_md5;
Int I;
If (fp = NULL ){
Fprintf (stderr, "fopen % s failed \ n", path );
Return NULL;
}
MD5Init (& mdContext );
While (bytes = fread (data, 1, 1024, fp ))! = 0)
{
MD5Update (& mdContext, data, bytes );
}
MD5Final (& mdContext );

File_md5 = (char *) malloc (md5_len + 1) * sizeof (char ));
If (file_md5 = NULL)
{
Fprintf (stderr, "malloc failed. \ n ");
Return NULL;
}
Memset (file_md5, 0, (md5_len + 1 ));

If (md5_len = 16)
{
For (I = 4; I <12; I ++)
{
Sprintf (& file_md5 [(I-4) * 2], "% 02x", mdContext. digest [I]);
}
}
Else if (md5_len = 32)
{
For (I = 0; I <16; I ++)
{
Sprintf (& file_md5 [I * 2], "% 02x", mdContext. digest [I]);
}
}
Else
{
Fclose (fp );
Free (file_md5 );
Return NULL;
}

Fclose (fp );
Return file_md5;
}
Int main (int argc, char * argv [])
{
Char * md5;

Md5 = MD5_file ("temp", 16 );
Printf ("16: % s \ n", md5 );
Free (md5 );

Md5 = MD5_file ("temp", 32 );
Printf ("32: % s \ n", md5 );
Free (md5 );
Return 0;
}

The following is the source code of md5.c.Copy codeThe Code is as follows: # include "md5.h"
/*
**************************************** ******************************
** Md5.c **
** RSA Data Security, Inc. MD5 Message Digest Algorithm **
** Created: 2/17/90 RLR **
** Revised: 1/91 SRD, AJ, BSK, JT Reference C Version **
**************************************** ******************************
*/
/*
**************************************** ******************************
** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved .**
****
** License to copy and use this software is granted provided that **
** It is identified as the "RSA Data Security, Inc. MD5 Message **
** Digest Algorithm "in all material mentioning or referencing this **
** Software or this function .**
****
** License is also granted to make and use derivative works **
** Provided that such works are identified as "derived from the RSA **
** Data Security, Inc. MD5 Message Digest Algorithm "in all **
** Material mentioning or referencing the derived work .**
****
** RSA Data Security, Inc. makes no representations concerning **
** Either the merchantability of this software or the suitability **
** Of this software for any particle purpose. It is provided "**
** Is "without express or implied warranty of any kind .**
****
** These notices must be retained in any copies of any part of this **
** Documentation and/or software .**
**************************************** ******************************
*/
/* -- Include the following line if the md5.h header file is separate --*/
/* # Include "md5.h "*/
/* Forward declaration */
Static void Transform ();
Static unsigned char PADDING [64] = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* F, G and H are basic MD5 functions: selection, majority, parity */
# Define F (x, y, z) (x) & (y) | ((~ X) & (z )))
# Define G (x, y, z) (x) & (z) | (y )&(~ Z )))
# Define H (x, y, z) (x) ^ (y) ^ (z ))
# Define I (x, y, z) (y) ^ (x) | (~ Z )))
/* ROTATE_LEFT rotates x left n bits */
# Define ROTATE_LEFT (x, n) (x) <(n) | (x)> (32-(n ))))
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
/* Rotation is separate from addition to prevent recomputation */
# Define FF (a, B, c, d, x, s, ac )\
{(A) + = F (B), (c), (d) + (x) + (UINT4) (ac );\
(A) = ROTATE_LEFT (a), (s ));\
(A) + = (B );\
}
# Define GG (a, B, c, d, x, s, ac )\
{(A) + = G (B), (c), (d) + (x) + (UINT4) (ac );\
(A) = ROTATE_LEFT (a), (s ));\
(A) + = (B );\
}
# Define HH (a, B, c, d, x, s, ac )\
{(A) + = H (B), (c), (d) + (x) + (UINT4) (ac );\
(A) = ROTATE_LEFT (a), (s ));\
(A) + = (B );\
}
# Define II (a, B, c, d, x, s, ac )\
{(A) + = I (B), (c), (d) + (x) + (UINT4) (ac );\
(A) = ROTATE_LEFT (a), (s ));\
(A) + = (B );\
}
Void MD5Init (MD5_CTX * mdContext)
{
MdContext-> I [0] = mdContext-> I [1] = (UINT4) 0;
/* Load magic initialization constants.
*/
MdContext-> buf [0] = (UINT4) 0x67452301;
MdContext-> buf [1] = (UINT4) 0xefcdab89;
MdContext-> buf [2] = (UINT4) 0x98badcfe;
MdContext-> buf [3] = (UINT4) 0x10325476;
}
Void MD5Update (MD5_CTX * mdContext, unsigned char * inBuf, unsigned int inLen)
{
UINT4 in [16];
Int mdi;
Unsigned int I, ii;
/* Compute number of bytes mod 64 */
Mdi = (int) (mdContext-> I [0]> 3) & 0x3F );
/* Update number of bits */
If (mdContext-> I [0] + (UINT4) inLen <3) <mdContext-> I [0])
MdContext-> I [1] ++;
MdContext-> I [0] + = (UINT4) inLen <3 );
MdContext-> I [1] + = (UINT4) inLen> 29 );
While (inLen --){
/* Add new character to buffer, increment mdi */
MdContext-> in [mdi ++] = * inBuf ++;
/* Transform if necessary */
If (mdi = 0x40 ){
For (I = 0, ii = 0; I <16; I ++, ii + = 4)
In [I] = (UINT4) mdContext-> in [ii + 3]) <24) |
(UINT4) mdContext-> in [ii + 2]) <16) |
(UINT4) mdContext-> in [ii + 1]) <8) |
(UINT4) mdContext-> in [ii]);
Transform (mdContext-> buf, in );
Mdi = 0;
}
}
}
Void MD5Final (MD5_CTX * mdContext)
{
UINT4 in [16];
Int mdi;
Unsigned int I, ii;
Unsigned int padLen;
/* Save number of bits */
In [14] = mdContext-> I [0];
In [15] = mdContext-> I [1];
/* Compute number of bytes mod 64 */
Mdi = (int) (mdContext-> I [0]> 3) & 0x3F );
/* Pad out to 56 mod 64 */
PadLen = (mdi <56 )? (56-mdi): (120-mdi );
MD5Update (mdContext, PADDING, padLen );
/* Append length in bits and transform */
For (I = 0, ii = 0; I <14; I ++, ii + = 4)
In [I] = (UINT4) mdContext-> in [ii + 3]) <24) |
(UINT4) mdContext-> in [ii + 2]) <16) |
(UINT4) mdContext-> in [ii + 1]) <8) |
(UINT4) mdContext-> in [ii]);
Transform (mdContext-> buf, in );
/* Store buffer in digest */
For (I = 0, ii = 0; I <4; I ++, ii + = 4 ){
MdContext-> digest [ii] = (unsigned char) (mdContext-> buf [I] & 0xFF );
MdContext-> digest [ii + 1] =
(Unsigned char) (mdContext-> buf [I]> 8) & 0xFF );
MdContext-> digest [ii + 2] =
(Unsigned char) (mdContext-> buf [I]> 16) & 0xFF );
MdContext-> digest [ii + 3] =
(Unsigned char) (mdContext-> buf [I]> 24) & 0xFF );
}
}
/* Basic MD5 step. Transform buf based on in.
*/
Static void Transform (UINT4 * buf, UINT4 * in)
{
UINT4 a = buf [0], B = buf [1], c = buf [2], d = buf [3];
/* Round 1 */
# Define S11 7
# Define S12 12
# Define S13 17
# Define S14 22
FF (a, B, c, d, in [0], S11, 3614090360);/* 1 */
FF (d, a, B, c, in [1], S12, 3905402710);/* 2 */
FF (c, d, a, B, in [2], S13, 606105819);/* 3 */
FF (B, c, d, a, in [3], S14, 3250441966);/* 4 */
FF (a, B, c, d, in [4], S11, 4118548399);/* 5 */
FF (d, a, B, c, in [5], S12, 1200080426);/* 6 */
FF (c, d, a, B, in [6], S13, 2821735955);/* 7 */
FF (B, c, d, a, in [7], S14, 4249261313);/* 8 */
FF (a, B, c, d, in [8], S11, 1770035416);/* 9 */
FF (d, a, B, c, in [9], S12, 2336552879);/* 10 */
FF (c, d, a, B, in [10], S13, 4294925233);/* 11 */
FF (B, c, d, a, in [11], S14, 2304563134);/* 12 */
FF (a, B, c, d, in [12], S11, 1804603682);/* 13 */
FF (d, a, B, c, in [13], S12, 4254626195);/* 14 */
FF (c, d, a, B, in [14], S13, 2792965006);/* 15 */
FF (B, c, d, a, in [15], S14, 1236535329);/* 16 */
/* Round 2 */
# Define S21 5
# Define S22 9
# Define S23 14
# Define S24 20
GG (a, B, c, d, in [1], S21, 4129170786);/* 17 */
GG (d, a, B, c, in [6], S22, 3225465664);/* 18 */
GG (c, d, a, B, in [11], S23, 643717713);/* 19 */
GG (B, c, d, a, in [0], S24, 3921069994);/* 20 */
GG (a, B, c, d, in [5], S21, 3593408605);/* 21 */
GG (d, a, B, c, in [10], S22, 38016083);/* 22 */
GG (c, d, a, B, in [15], S23, 3634488961);/* 23 */
GG (B, c, d, a, in [4], S24, 3889429448);/* 24 */
GG (a, B, c, d, in [9], S21, 568446438);/* 25 */
GG (d, a, B, c, in [14], S22, 3275163606);/* 26 */
GG (c, d, a, B, in [3], S23, 4107603335);/* 27 */
GG (B, c, d, a, in [8], S24, 1163531501);/* 28 */
GG (a, B, c, d, in [13], S21, 2850285829);/* 29 */
GG (d, a, B, c, in [2], S22, 4243563512);/* 30 */
GG (c, d, a, B, in [7], S23, 1735328473);/* 31 */
GG (B, c, d, a, in [12], S24, 2368359562);/* 32 */
/* Round 3 */
# Define S31 4
# Define S32 11
# Define S33 16
# Define S34 23
HH (a, B, c, d, in [5], S31, 4294588738);/* 33 */
HH (d, a, B, c, in [8], S32, 2272392833);/* 34 */
HH (c, d, a, B, in [11], S33, 1839030562);/* 35 */
HH (B, c, d, a, in [14], S34, 4259657740);/* 36 */
HH (a, B, c, d, in [1], S31, 2763975236);/* 37 */
HH (d, a, B, c, in [4], S32, 1272893353);/* 38 */
HH (c, d, a, B, in [7], S33, 4139469664);/* 39 */
HH (B, c, d, a, in [10], S34, 3200236656);/* 40 */
HH (a, B, c, d, in [13], S31, 681279174);/* 41 */
HH (d, a, B, c, in [0], S32, 3936430074);/* 42 */
HH (c, d, a, B, in [3], S33, 3572445317);/* 43 */
HH (B, c, d, a, in [6], S34, 76029189);/* 44 */
HH (a, B, c, d, in [9], S31, 3654602809);/* 45 */
HH (d, a, B, c, in [12], S32, 3873151461);/* 46 */
HH (c, d, a, B, in [15], S33, 530742520);/* 47 */
HH (B, c, d, a, in [2], S34, 3299628645);/* 48 */
/* Round 4 */
# Define S41 6
# Define S42 10
# Define S43 15
# Define S44 21
II (a, B, c, d, in [0], S41, 4096336452);/* 49 */
II (d, a, B, c, in [7], S42, 1126891415);/* 50 */
II (c, d, a, B, in [14], S43, 2878612391);/* 51 */
II (B, c, d, a, in [5], S44, 4237533241);/* 52 */
II (a, B, c, d, in [12], S41, 1700485571);/* 53 */
II (d, a, B, c, in [3], S42, 2399980690);/* 54 */
II (c, d, a, B, in [10], S43, 4293915773);/* 55 */
II (B, c, d, a, in [1], S44, 2240044497);/* 56 */
II (a, B, c, d, in [8], S41, 1873313359);/* 57 */
II (d, a, B, c, in [15], S42, 4264355552);/* 58 */
II (c, d, a, B, in [6], S43, 2734768916);/* 59 */
II (B, c, d, a, in [13], S44, 1309151649);/* 60 */
II (a, B, c, d, in [4], S41, 4149444226);/* 61 */
II (d, a, B, c, in [11], S42, 3174756917);/* 62 */
II (c, d, a, B, in [2], S43, 718787259);/* 63 */
II (B, c, d, a, in [9], S44, 3951481745);/* 64 */
Buf [0] + =;
Buf [1] + = B;
Buf [2] + = c;
Buf [3] + = d;
}
/*
**************************************** ******************************
** End of md5.c **
* ****************************** (Cut) ********************************
*/

The following is the source code of md5.h:Copy codeThe Code is as follows :/*
**************************************** ******************************
** Md5.h -- Header file for implementation of MD5 **
** RSA Data Security, Inc. MD5 Message Digest Algorithm **
** Created: 2/17/90 RLR **
** Revised: 12/27/90 SRD, AJ, BSK, JT Reference C version **
** Revised (for MD5): RLR 4/27/91 **
** -- G modified to have y &~ Z instead of y & z **
** -- FF, GG, HH modified to add in last register done **
** -- Access pattern: round 2 works mod 5, round 3 works mod 3 **
** -- Distinct additive constant for each step **
** -- Round 4 added, working mod 7 **
**************************************** ******************************
*/
/*
**************************************** ******************************
** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved .**
****
** License to copy and use this software is granted provided that **
** It is identified as the "RSA Data Security, Inc. MD5 Message **
** Digest Algorithm "in all material mentioning or referencing this **
** Software or this function .**
****
** License is also granted to make and use derivative works **
** Provided that such works are identified as "derived from the RSA **
** Data Security, Inc. MD5 Message Digest Algorithm "in all **
** Material mentioning or referencing the derived work .**
****
** RSA Data Security, Inc. makes no representations concerning **
** Either the merchantability of this software or the suitability **
** Of this software for any particle purpose. It is provided "**
** Is "without express or implied warranty of any kind .**
****
** These notices must be retained in any copies of any part of this **
** Documentation and/or software .**
**************************************** ******************************
*/
/* Typedef a 32 bit type */
Typedef unsigned long int UINT4;
/* Data structure for MD5 (Message Digest) computation */
Typedef struct {
UINT4 I [2];/* number of _ bits _ handled mod 2 ^ 64 */
UINT4 buf [4];/* scratch buffer */
Unsigned char in [64];/* input buffer */
Unsigned char digest [16];/* actual digest after MD5Final call */
} MD5_CTX;
Void MD5Init ();
Void MD5Update ();
Void MD5Final ();
/*
**************************************** ******************************
** End of md5.h **
* ****************************** (Cut) ********************************
*/

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.