00003 不思议迷宫.0010.2:project.manifest自动生成器

来源:互联网 发布:编程培训班 编辑:程序博客网 时间:2024/04/28 23:37


00003不思议迷宫.0010.2project.manifest自动生成器

每次修改完代码,在安装到手机之前,老是手动执行如下操作:

l        VS中以二进制方式打开luac文件,去除BOM

l        找到修改的luac文件,将之拖动到MD5工具

l        md5工具中,复制md5的值

l        md5值拷贝到文本文件中进行比较,然后拷贝第x位到第y位的值

l        打开project.manifest,找到luac文件对应的项目,复制第x位到第y位的值

l        回到md5工具,查看luac文件的size

l        再回到project.manifest,修改size

每次更新,修改的往往不仅一个文件,这就痛苦了。作为一个懒惰的人,我只想点个鼠标就统统搞定一切。这次,让我来一劳永逸地解决这个问题。

project.manifest是个ANSI编码的文本文件,里面有固定的内容和需要动态生成的md5内容。随着游戏版本的不同,固定内容可能会有所不同;所以,固定内容不能在生成器程序中写死,我们用模版文件,比如:

固定内容1;动态md5内容;固定内容2

生成器读取到模版内容,自动生成md5内容后,将“动态md5内容;”替换为真实的内容,然后写回到project.manifest

自动生成md5内容也没什么难的,就是枚举目录和文件,获得相对路径,然后计算md5值,取其中的一个特定子串,并获得大小,一起组合为一个json字符串。

计算文件md5值的代码,网上有现成的。我找的这个是C写的,虽然看得不大懂,但好在能用。

运行,发现非常慢。这也就算了,关键是计算出来的值和其他工具计算出来的不一样,真是蛋疼。又从网上找了一个,这回对了:

//Magicinitialization constants

#defineMD5_INIT_STATE_0 0x67452301

#defineMD5_INIT_STATE_1 0xefcdab89

#defineMD5_INIT_STATE_2 0x98badcfe

#defineMD5_INIT_STATE_3 0x10325476

 

//Constants forTransform routine.

#defineMD5_S11 7

#define MD5_S1212

#define MD5_S1317

#define MD5_S1422

#defineMD5_S21 5

#defineMD5_S22 9

#define MD5_S2314

#define MD5_S2420

#defineMD5_S31 4

#define MD5_S3211

#define MD5_S3316

#define MD5_S3423

#defineMD5_S41 6

#define MD5_S4210

#define MD5_S4315

#define MD5_S4421

 

//TransformationConstants - Round 1

#defineMD5_T01 0xd76aa478 //TransformationConstant 1

#defineMD5_T02 0xe8c7b756 //Transformation Constant 2

#defineMD5_T03 0x242070db //TransformationConstant 3

#defineMD5_T04 0xc1bdceee //TransformationConstant 4

#defineMD5_T05 0xf57c0faf //Transformation Constant 5

#defineMD5_T06 0x4787c62a//Transformation Constant 6

#defineMD5_T07 0xa8304613 //TransformationConstant 7

#defineMD5_T08 0xfd469501 //TransformationConstant 8

#defineMD5_T09 0x698098d8 //TransformationConstant 9

#defineMD5_T10 0x8b44f7af //Transformation Constant 10

#defineMD5_T11 0xffff5bb1 //TransformationConstant 11

#defineMD5_T12 0x895cd7be //TransformationConstant 12

#defineMD5_T13 0x6b901122 //TransformationConstant 13

#defineMD5_T14 0xfd987193 //TransformationConstant 14

#defineMD5_T15 0xa679438e //TransformationConstant 15

#defineMD5_T16 0x49b40821 //TransformationConstant 16

 

//TransformationConstants - Round 2

#defineMD5_T17 0xf61e2562 //TransformationConstant 17

#defineMD5_T18 0xc040b340 //TransformationConstant 18

#defineMD5_T19 0x265e5a51 //Transformation Constant 19

#defineMD5_T20 0xe9b6c7aa //Transformation Constant 20

#defineMD5_T21 0xd62f105d //Transformation Constant 21

#defineMD5_T22 0x02441453 //TransformationConstant 22

#defineMD5_T23 0xd8a1e681 //Transformation Constant 23

#defineMD5_T24 0xe7d3fbc8 //TransformationConstant 24

#defineMD5_T25 0x21e1cde6 //TransformationConstant 25

#defineMD5_T26 0xc33707d6 //TransformationConstant 26

#defineMD5_T27 0xf4d50d87 //TransformationConstant 27

#defineMD5_T28 0x455a14ed //Transformation Constant 28

#defineMD5_T29 0xa9e3e905 //TransformationConstant 29

#defineMD5_T30 0xfcefa3f8 //Transformation Constant 30

#defineMD5_T31 0x676f02d9 //Transformation Constant 31

#defineMD5_T32 0x8d2a4c8a //Transformation Constant 32

 

//TransformationConstants - Round 3

#defineMD5_T33 0xfffa3942 //TransformationConstant 33

#defineMD5_T34 0x8771f681 //Transformation Constant 34

#defineMD5_T35 0x6d9d6122 //TransformationConstant 35

#defineMD5_T36 0xfde5380c //Transformation Constant 36

#defineMD5_T37 0xa4beea44 //TransformationConstant 37

#defineMD5_T38 0x4bdecfa9 //Transformation Constant38

#defineMD5_T39 0xf6bb4b60 //TransformationConstant 39

#defineMD5_T40 0xbebfbc70 //TransformationConstant 40

#defineMD5_T41 0x289b7ec6 //TransformationConstant 41

#defineMD5_T42 0xeaa127fa //TransformationConstant 42

#defineMD5_T43 0xd4ef3085 //TransformationConstant 43

#defineMD5_T44 0x04881d05 //TransformationConstant 44

#defineMD5_T45 0xd9d4d039 //TransformationConstant 45

#defineMD5_T46 0xe6db99e5 //TransformationConstant 46

#defineMD5_T47 0x1fa27cf8 //TransformationConstant 47

#defineMD5_T48 0xc4ac5665 //Transformation Constant 48

 

//TransformationConstants - Round 4

#defineMD5_T49 0xf4292244 //TransformationConstant 49

#defineMD5_T50 0x432aff97 //TransformationConstant 50

#defineMD5_T51 0xab9423a7 //Transformation Constant 51

#defineMD5_T52 0xfc93a039 //Transformation Constant 52

#defineMD5_T53 0x655b59c3 //Transformation Constant 53

#defineMD5_T54 0x8f0ccc92 //Transformation Constant 54

#defineMD5_T55 0xffeff47d //Transformation Constant55

#defineMD5_T56 0x85845dd1 //TransformationConstant 56

#defineMD5_T57 0x6fa87e4f //Transformation Constant 57

#defineMD5_T58 0xfe2ce6e0 //TransformationConstant 58

#defineMD5_T59 0xa3014314 //TransformationConstant 59

#defineMD5_T60 0x4e0811a1 //Transformation Constant 60

#defineMD5_T61 0xf7537e82 //TransformationConstant 61

#defineMD5_T62 0xbd3af235 //TransformationConstant 62

#defineMD5_T63 0x2ad7d2bb //TransformationConstant 63

#defineMD5_T64 0xeb86d391 //Transformation Constant64

 

 

//Null data(except for first BYTE) used to finalise the checksum calculation

static unsignedchar PADDING[64] = {

 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0,

 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0,

 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0

};

CMD5Checksum.h

#if!defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)

#defineAFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_

 

#if _MSC_VER> 1000

#pragma once

#endif //_MSC_VER > 1000

/*****************************************************************************************

 

 

*****************************************************************************************/

classCMD5Checksum 

{

public:

 static CString GetMD5OfString(CStringstrString);

 //interface functions for the RSA MD5calculation

 static CString GetMD5(const CString&strFilePath);

 

protected:

 //constructor/destructor

 CMD5Checksum();

 virtual ~CMD5Checksum() {};

 

 //RSA MD5 implementation

 void Transform(BYTE Block[64]);

 void Update(BYTE* Input, ULONG nInputLen);

 CString Final();

 inline DWORD RotateLeft(DWORD x, int n);

 inline void FF( DWORD& A, DWORD B, DWORDC, DWORD D, DWORD X, DWORD S, DWORD T);

 inline void GG( DWORD& A, DWORD B, DWORDC, DWORD D, DWORD X, DWORD S, DWORD T);

 inline void HH( DWORD& A, DWORD B, DWORDC, DWORD D, DWORD X, DWORD S, DWORD T);

 inline void II( DWORD& A, DWORD B, DWORDC, DWORD D, DWORD X, DWORD S, DWORD T);

 

 //utility functions

 inline void DWordToByte(BYTE* Output, DWORD*Input, UINT nLength);

 inline void ByteToDWord(DWORD* Output, BYTE*Input, UINT nLength);

 

private:

 BYTE m_lpszBuffer[64]; //input buffer

 ULONG m_nCount[2];  //number of bits, modulo 2^64 (lsb first)

 ULONG m_lMD5[4];  //MD5 checksum

};

 

#endif //!defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)

 

MD5Checksum.cpp

#include"stdafx.h"

#include"MD5Checksum.h"

#include"MD5ChecksumDefines.h"

 

#ifdef _DEBUG

#undef THIS_FILE

static charTHIS_FILE[]=__FILE__;

#define newDEBUG_NEW

#endif

 

 

/*****************************************************************************************

 

*****************************************************************************************/

CStringCMD5Checksum::GetMD5(const CString& strFilePath)

{

      try

      {

             CFile file;

             if(file.Open(strFilePath,CFile::modeRead)==0)

                    return _T("");

 

             CMD5Checksum MD5Checksum;  //checksum object

             int nLength = 0;      //number of bytes read from the file

             const int nBufferSize = 1024;//checksum the file in blocks of 1024 bytes

             BYTE Buffer[nBufferSize];  //buffer for data read from the file

 

             //checksum the file in blocks of1024 bytes

             while ((nLength = file.Read(Buffer, nBufferSize )) > 0 )

             {

                    MD5Checksum.Update( Buffer,nLength );

             }

 

             file.Close();

 

             //finalise the checksum and returnit

             return MD5Checksum.Final();

      }

 

      //report any file exceptions in debugmode only

      catch (CFileException* e )

      {

             TRACE0("CMD5Checksum::GetMD5:CFileException caught");

             throw e;

      }

}

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::RotateLeft

DETAILS: private

DESCRIPTION:Rotates the bits in a 32 bit DWORD left by a specified amount

RETURNS: The rotated DWORD

ARGUMENTS: DWORD x : the value to be rotated

int n  : the number of bits to rotate by

*****************************************************************************************/

DWORDCMD5Checksum::RotateLeft(DWORD x, int n)

{

      //check that DWORD is 4 bytes long - truein Visual C++ 6 and 32 bit Windows

      ASSERT( sizeof(x) == 4 );

 

      //rotate and return x

      return (x << n) | (x >>(32-n));

}

 

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::FF

DETAILS: protected

DESCRIPTION:Implementation of basic MD5 transformation algorithm

RETURNS: none

ARGUMENTS: DWORD &A, B, C, D : Current (partial)checksum

DWORD X          : Input data

DWORD S    : MD5_SXX Transformation constant

DWORD T    : MD5_TXX Transformation constant

NOTES:  None

*****************************************************************************************/

voidCMD5Checksum::FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S,DWORD T)

{

      DWORD F = (B & C) | (~B & D);

      A += F + X + T;

      A = RotateLeft(A, S);

      A += B;

}

 

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::GG

DETAILS: protected

DESCRIPTION:Implementation of basic MD5 transformation algorithm

RETURNS: none

ARGUMENTS: DWORD &A, B, C, D : Current (partial)checksum

DWORD X          : Input data

DWORD S     : MD5_SXX Transformation constant

DWORD T     : MD5_TXX Transformation constant

NOTES:  None

*****************************************************************************************/

voidCMD5Checksum::GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S,DWORD T)

{

      DWORD G = (B & D) | (C & ~D);

      A += G + X + T;

      A = RotateLeft(A, S);

      A += B;

}

 

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::HH

DETAILS: protected

DESCRIPTION:Implementation of basic MD5 transformation algorithm

RETURNS: none

ARGUMENTS: DWORD &A, B, C, D : Current (partial)checksum

DWORD X          : Input data

DWORD S     : MD5_SXX Transformation constant

DWORD T     : MD5_TXX Transformation constant

NOTES:  None

*****************************************************************************************/

voidCMD5Checksum::HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S,DWORD T)

{

      DWORD H = (B ^ C ^ D);

      A += H + X + T;

      A = RotateLeft(A, S);

      A += B;

}

 

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::II

DETAILS: protected

DESCRIPTION:Implementation of basic MD5 transformation algorithm

RETURNS: none

ARGUMENTS: DWORD &A, B, C, D : Current (partial)checksum

DWORD X          : Input data

DWORD S     : MD5_SXX Transformation constant

DWORD T     : MD5_TXX Transformation constant

NOTES:  None

*****************************************************************************************/

voidCMD5Checksum::II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S,DWORD T)

{

      DWORD I = (C ^ (B | ~D));

      A += I + X + T;

      A = RotateLeft(A, S);

      A += B;

}

 

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::ByteToDWord

DETAILS: private

DESCRIPTION:Transfers the data in an 8 bit array to a 32 bit array

RETURNS: void

ARGUMENTS: DWORD* Output : the 32 bit (unsigned long)destination array

BYTE* Input  : the 8 bit (unsigned char) source array

UINTnLength : the number of 8 bit data itemsin the source array

NOTES:  Four BYTES from the input array aretransferred to each DWORD entry

of the outputarray. The first BYTE is transferred to the bits (0-7)

of the outputDWORD, the second BYTE to bits 8-15 etc.

The algorithmassumes that the input array is a multiple of 4 bytes long

so that there isa perfect fit into the array of 32 bit words.

*****************************************************************************************/

voidCMD5Checksum::ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength)

{

      //entry invariants

      ASSERT( nLength % 4 == 0 );

      ASSERT( AfxIsValidAddress(Output,nLength/4, TRUE) );

      ASSERT( AfxIsValidAddress(Input, nLength,FALSE) );

 

      //initialisations

      UINT i=0; //index to Output array

      UINT j=0; //index to Input array

 

      //transfer the data by shifting andcopying

      for ( ; j < nLength; i++, j += 4)

      {

             Output[i] = (ULONG)Input[j]  |

                    (ULONG)Input[j+1] <<8 |

                    (ULONG)Input[j+2] <<16 |

                    (ULONG)Input[j+3] <<24;

      }

}

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::Transform

DETAILS: protected

DESCRIPTION: MD5basic transformation algorithm; transforms 'm_lMD5'

RETURNS: void

ARGUMENTS: BYTE Block[64]

NOTES:  An MD5 checksum is calculated by four roundsof 'Transformation'.

The MD5 checksumcurrently held in m_lMD5 is merged by the

transformationprocess with data passed in 'Block'. 

*****************************************************************************************/

voidCMD5Checksum::Transform(BYTE Block[64])

{

      //initialise local data with currentchecksum

      ULONG a = m_lMD5[0];

      ULONG b = m_lMD5[1];

      ULONG c = m_lMD5[2];

      ULONG d = m_lMD5[3];

 

      //copy BYTES from input 'Block' to anarray of ULONGS 'X'

      ULONG X[16];

      ByteToDWord( X, Block, 64 );

 

      //Perform Round 1 of the transformation

      FF (a, b, c, d, X[ 0], MD5_S11, MD5_T01);

      FF (d, a, b, c, X[ 1], MD5_S12, MD5_T02);

      FF (c, d, a, b, X[ 2], MD5_S13, MD5_T03);

      FF (b, c, d, a, X[ 3], MD5_S14, MD5_T04);

      FF (a, b, c, d, X[ 4], MD5_S11, MD5_T05);

      FF (d, a, b, c, X[ 5], MD5_S12, MD5_T06);

      FF (c, d, a, b, X[ 6], MD5_S13, MD5_T07);

      FF (b, c, d, a, X[ 7], MD5_S14, MD5_T08);

      FF (a, b, c, d, X[ 8], MD5_S11, MD5_T09);

      FF (d, a, b, c, X[ 9], MD5_S12, MD5_T10);

      FF (c, d, a, b, X[10], MD5_S13, MD5_T11);

      FF (b, c, d, a, X[11], MD5_S14, MD5_T12);

      FF (a, b, c, d, X[12], MD5_S11, MD5_T13);

      FF (d, a, b, c, X[13], MD5_S12, MD5_T14);

      FF (c, d, a, b, X[14], MD5_S13, MD5_T15);

      FF (b, c, d, a, X[15], MD5_S14, MD5_T16);

 

      //Perform Round 2 of the transformation

      GG (a, b, c, d, X[ 1], MD5_S21, MD5_T17);

      GG (d, a, b, c, X[ 6], MD5_S22, MD5_T18);

      GG (c, d, a, b, X[11], MD5_S23, MD5_T19);

      GG (b, c, d, a, X[ 0], MD5_S24, MD5_T20);

      GG (a, b, c, d, X[ 5], MD5_S21, MD5_T21);

      GG (d, a, b, c, X[10], MD5_S22, MD5_T22);

      GG (c, d, a, b, X[15], MD5_S23, MD5_T23);

      GG (b, c, d, a, X[ 4], MD5_S24, MD5_T24);

      GG (a, b, c, d, X[ 9], MD5_S21, MD5_T25);

      GG (d, a, b, c, X[14], MD5_S22, MD5_T26);

      GG (c, d, a, b, X[ 3], MD5_S23, MD5_T27);

      GG (b, c, d, a, X[ 8], MD5_S24, MD5_T28);

      GG (a, b, c, d, X[13], MD5_S21, MD5_T29);

      GG (d, a, b, c, X[ 2], MD5_S22, MD5_T30);

      GG (c, d, a, b, X[ 7], MD5_S23, MD5_T31);

      GG (b, c, d, a, X[12], MD5_S24, MD5_T32);

 

      //Perform Round 3 of the transformation

      HH (a, b, c, d, X[ 5], MD5_S31, MD5_T33);

      HH (d, a, b, c, X[ 8], MD5_S32, MD5_T34);

      HH (c, d, a, b, X[11], MD5_S33, MD5_T35);

      HH (b, c, d, a, X[14], MD5_S34, MD5_T36);

      HH (a, b, c, d, X[ 1], MD5_S31, MD5_T37);

      HH (d, a, b, c, X[ 4], MD5_S32, MD5_T38);

      HH (c, d, a, b, X[ 7], MD5_S33, MD5_T39);

      HH (b, c, d, a, X[10], MD5_S34, MD5_T40);

      HH (a, b, c, d, X[13], MD5_S31, MD5_T41);

      HH (d, a, b, c, X[ 0], MD5_S32, MD5_T42);

      HH (c, d, a, b, X[ 3], MD5_S33, MD5_T43);

      HH (b, c, d, a, X[ 6], MD5_S34, MD5_T44);

      HH (a, b, c, d, X[ 9], MD5_S31, MD5_T45);

      HH (d, a, b, c, X[12], MD5_S32, MD5_T46);

      HH (c, d, a, b, X[15], MD5_S33, MD5_T47);

      HH (b, c, d, a, X[ 2], MD5_S34, MD5_T48);

 

      //Perform Round 4 of the transformation

      II (a, b, c, d, X[ 0], MD5_S41, MD5_T49);

      II (d, a, b, c, X[ 7], MD5_S42, MD5_T50);

      II (c, d, a, b, X[14], MD5_S43, MD5_T51);

      II (b, c, d, a, X[ 5], MD5_S44, MD5_T52);

      II (a, b, c, d, X[12], MD5_S41, MD5_T53);

      II (d, a, b, c, X[ 3], MD5_S42, MD5_T54);

      II (c, d, a, b, X[10], MD5_S43, MD5_T55);

      II (b, c, d, a, X[ 1], MD5_S44, MD5_T56);

      II (a, b, c, d, X[ 8], MD5_S41, MD5_T57);

      II (d, a, b, c, X[15], MD5_S42, MD5_T58);

      II (c, d, a, b, X[ 6], MD5_S43, MD5_T59);

      II (b, c, d, a, X[13], MD5_S44, MD5_T60);

      II (a, b, c, d, X[ 4], MD5_S41, MD5_T61);

      II (d, a, b, c, X[11], MD5_S42, MD5_T62);

      II (c, d, a, b, X[ 2], MD5_S43, MD5_T63);

      II (b, c, d, a, X[ 9], MD5_S44, MD5_T64);

 

      //add the transformed values to thecurrent checksum

      m_lMD5[0] += a;

      m_lMD5[1] += b;

      m_lMD5[2] += c;

      m_lMD5[3] += d;

}

 

 

/*****************************************************************************************

CONSTRUCTOR:CMD5Checksum

DESCRIPTION:Initialises member data

ARGUMENTS: None

NOTES:  None

*****************************************************************************************/

CMD5Checksum::CMD5Checksum()

{

      // zero members

      memset( m_lpszBuffer, 0, 64 );

      m_nCount[0] = m_nCount[1] = 0;

 

      // Load magic state initializationconstants

      m_lMD5[0] = MD5_INIT_STATE_0;

      m_lMD5[1] = MD5_INIT_STATE_1;

      m_lMD5[2] = MD5_INIT_STATE_2;

      m_lMD5[3] = MD5_INIT_STATE_3;

}

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::DWordToByte

DETAILS: private

DESCRIPTION:Transfers the data in an 32 bit array to a 8 bit array

RETURNS: void

ARGUMENTS: BYTE* Output : the 8 bit destination array

DWORD*Input : the 32 bit source array

UINTnLength : the number of 8 bit data itemsin the source array

NOTES:  One DWORD from the input array istransferred into four BYTES

in the outputarray. The first (0-7) bits of the first DWORD are

transferred tothe first output BYTE, bits bits 8-15 are transferred from

the second BYTEetc.

 

The algorithmassumes that the output array is a multiple of 4 bytes long

so that there isa perfect fit of 8 bit BYTES into the 32 bit DWORDs.

*****************************************************************************************/

voidCMD5Checksum::DWordToByte(BYTE* Output, DWORD* Input, UINT nLength )

{

      //entry invariants

      ASSERT( nLength % 4 == 0 );

      ASSERT( AfxIsValidAddress(Output,nLength, TRUE) );

      ASSERT( AfxIsValidAddress(Input,nLength/4, FALSE) );

 

      //transfer the data by shifting andcopying

      UINT i = 0;

      UINT j = 0;

      for ( ; j < nLength; i++, j += 4)

      {

             Output[j] = (UCHAR)(Input[i] & 0xff   );

             Output[j+1] = (UCHAR)((Input[i]>> 8) & 0xff);

             Output[j+2] = (UCHAR)((Input[i]>> 16) & 0xff);

             Output[j+3] = (UCHAR)((Input[i]>> 24) & 0xff);

      }

}

 

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::Final

DETAILS: protected

DESCRIPTION:Implementation of main MD5 checksum algorithm; ends the checksum calculation.

RETURNS: CString : the final hexadecimal MD5 checksumresult

ARGUMENTS: None

NOTES:  Performs the final MD5 checksum calculation('Update' does most of the work,

this functionjust finishes the calculation.)

*****************************************************************************************/

CStringCMD5Checksum::Final()

{

      //Save number of bits

      BYTE Bits[8];

      DWordToByte( Bits, m_nCount, 8 );

 

      //Pad out to 56 mod 64.

      UINT nIndex = (UINT)((m_nCount[0]>> 3) & 0x3f);

      UINT nPadLen = (nIndex < 56) ? (56 -nIndex) : (120 - nIndex);

      Update( PADDING, nPadLen );

 

      //Append length (before padding)

      Update( Bits, 8 );

 

      //Store final state in 'lpszMD5'

      const int nMD5Size = 16;

      unsigned char lpszMD5[ nMD5Size ];

      DWordToByte( lpszMD5, m_lMD5, nMD5Size );

 

      //Convert the hexadecimal checksum to aCString

      CString strMD5;

      for ( int i=0; i < nMD5Size; i++)

      {

             CString Str;

             if (lpszMD5[i] == 0)

             {

                    Str =CString("00");

             }

             else if (lpszMD5[i] <= 15) 

             {

                    Str.Format(_T("0%X"),lpszMD5[i]);

             }

             else

             {

                    Str.Format(_T("%X"),lpszMD5[i]);

             }

 

             ASSERT( Str.GetLength() == 2 );

             strMD5 += Str;

      }

      ASSERT( strMD5.GetLength() == 32 );

      return strMD5;

}

 

 

/*****************************************************************************************

FUNCTION: CMD5Checksum::Update

DETAILS: protected

DESCRIPTION:Implementation of main MD5 checksum algorithm

RETURNS: void

ARGUMENTS: BYTE* Input   : input block

UINT nInputLen :length of input block

NOTES:  Computes the partial MD5 checksum for 'nInputLen'bytes of data in 'Input'

*****************************************************************************************/

voidCMD5Checksum::Update( BYTE* Input, ULONG nInputLen )

{

      //Compute number of bytes mod 64

      UINT nIndex = (UINT)((m_nCount[0] >>3) & 0x3F);

 

      //Update number of bits

      if ( ( m_nCount[0] += nInputLen <<3 ) < ( nInputLen << 3) )

      {

             m_nCount[1]++;

      }

      m_nCount[1] += (nInputLen >> 29);

 

      //Transform as many times as possible.

      UINT i=0; 

      UINT nPartLen = 64 - nIndex;

      if (nInputLen >= nPartLen) 

      {

             memcpy( &m_lpszBuffer[nIndex],Input, nPartLen );

             Transform( m_lpszBuffer );

             for (i = nPartLen; i + 63 <nInputLen; i += 64)

             {

                    Transform( &Input[i] );

             }

             nIndex = 0;

      }

      else

      {

             i = 0;

      }

 

      // Buffer remaining input

      memcpy( &m_lpszBuffer[nIndex],&Input[i], nInputLen-i);

}

 

 

 

CStringCMD5Checksum::GetMD5OfString(CString strString)

{

      try

      {

             CMD5Checksum MD5Checksum;  //checksum object

             int nLength =strString.GetLength();      //number ofbytes read from the file

             //const int nBufferSize = 1024;//checksum the file in blocks of 1024 bytes

             BYTE *Buffer;  //buffer for data read from the file

             Buffer=(BYTE*)(strString.GetBuffer(nLength));

             //checksum the file in blocks of1024 bytes

             //while ((nLength = File.Read(Buffer, nBufferSize )) > 0 )

             //{

             MD5Checksum.Update( Buffer,nLength );

             //}

             //finalise the checksum and returnit

             return MD5Checksum.Final();

      }

 

      //report any file exceptions in debugmode only

      catch (CFileException* e )

      {

             TRACE0("CMD5Checksum::GetMD5:CFileException caught");

             throw e;

      }

}

0 0
原创粉丝点击