⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmmd5checksum.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   implementation of the MD5Checksum class. 

                   Note this file was originally downloaded as part of a project
                   found on the web. It has been entirely reformatted and cleaned
                   up so may not resemble the original file very much. Original
                   authors comments are shown below:

                   This software is derived from the RSA Data Security, Inc. 
                   MD5 Message-Digest Algorithm. Incorporation of this statement 
                   is a condition of use; please see the RSA Data Security Inc 
                   copyright notice below:-

                   Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
                   rights reserved.

                   RSA Data Security, Inc. makes no representations concerning either
                   the merchantability of this software or the suitability of this
                   software for any particular purpose. It is provided "as 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.

                   Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. 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 particular purpose. It is provided "as 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.

                   This implementation of the RSA MD5 Algorithm was written by Langfine Ltd.

                   Langfine Ltd makes no representations concerning either
                   the merchantability of this software or the suitability of this
                   software for any particular purpose. It is provided "as is"
                   without express or implied warranty of any kind.

                   In addition to the above, Langfine make no warrant or assurances 
                   regarding the accuracy of this implementation of the MD5 checksum
                   algorithm nor any assurances regarding its suitability for any purposes.

                   This implementation may be used freely provided that Langfine is 
                   credited in a copyright or similar notices (eg, RSA MD5 Algorithm 
                   implemented by Langfine Ltd.) and provided that the RSA Data Security 
                   notices are complied with.
*/
static char OBJECT_ID[] = "$Revision: 1 $ : $Date: 1/27/99 9:23a $";
/*****************************************************************************/

#include <windows.h>
#include <assert.h>
#include "VMMD5Checksum.h"
#include "VMMD5ChecksumDefines.h"


/*****************************************************************************/
/*
     FUNCTION NAME:  CHexEncoder::HexStringToBytes

       DESCRIPTION:  translates a hex-encoded string into the original byte 
                     array value represented by the hex string

             INPUT:  pchBuffer   : pointer to buffer to get the digest for
                     iLength     : length of the input buffer

           RETURNS:  VMString containing the hex-string representation 
                     of the digest value for the input buffer
*/
VMString VMMD5Checksum::GetMD5( char* pchBuffer, unsigned int iLength )
{
  // calculate and return the checksum
  //
  VMMD5Checksum oMD5;

  oMD5.Update( pchBuffer, iLength );

  return( oMD5.Final() );
}
/* End of function "CHexEncoder::HexStringToBytes"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMMD5Checksum::RotateLeft

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

             INPUT:  dwToRotate   : value to rotate
                     iRotateCount : number of bits to rotate by
            OUTPUT:  the rotated value

           RETURNS:  number of bytes loaded into the output array
*/
DWORD VMMD5Checksum::RotateLeft( DWORD dwToRotate, int iRotateCount )
{
  //check that DWORD is 4 bytes long - true in Visual C++ 6 and 32 bit Windows
  //
  assert( sizeof( dwToRotate ) == 4 );

  // rotate and return x
  //
  return ( dwToRotate << iRotateCount ) | ( dwToRotate >> ( 32 - iRotateCount ) );
}
/* End of function "VMMD5Checksum::RotateLeft"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMMD5Checksum::FF

       DESCRIPTION:  performs 'stage 1' of the MD5 transform 

             INPUT:  &A, B, C, D : Current (partial) checksum
                     X           : Input data
                     S           : VM_MD5_SXX Transformation constant
                     T           : VM_MD5_TXX Transformation constant
            OUTPUT:  A contents are modified

           RETURNS:  void
*/
void VMMD5Checksum::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;
}
/* End of function "VMMD5Checksum::FF"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMMD5Checksum::GG

       DESCRIPTION:  performs 'stage 2' of the MD5 transform 

             INPUT:  &A, B, C, D : Current (partial) checksum
                     X           : Input data
                     S           : VM_MD5_SXX Transformation constant
                     T           : VM_MD5_TXX Transformation constant
            OUTPUT:  A contents are modified

           RETURNS:  void
*/
void VMMD5Checksum::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;
}
/* End of function "VMMD5Checksum::GG"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMMD5Checksum::HH

       DESCRIPTION:  performs 'stage 3' of the MD5 transform 

             INPUT:  &A, B, C, D : Current (partial) checksum
                     X           : Input data
                     S           : VM_MD5_SXX Transformation constant
                     T           : VM_MD5_TXX Transformation constant
            OUTPUT:  A contents are modified

           RETURNS:  void
*/
void VMMD5Checksum::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;
}
/* End of function "CHexEncoder::VMMD5Checksum::HH"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMMD5Checksum::II

       DESCRIPTION:  performs 'stage 4' of the MD5 transform 

             INPUT:  &A, B, C, D : Current (partial) checksum
                     X           : Input data
                     S           : VM_MD5_SXX Transformation constant
                     T           : VM_MD5_TXX Transformation constant
            OUTPUT:  A contents are modified

           RETURNS:  void
*/
void VMMD5Checksum::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;
}
/* End of function "VMMD5Checksum::II"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMMD5Checksum::Transform

       DESCRIPTION:  MD5 basic transformation algorithm;  transforms 'm_aulMD5CheckSum'
                     An MD5 checksum is calculated by four rounds of 'Transformation'.
				         The MD5 checksum currently held in m_aulMD5CheckSum is merged by the 
				         transformation process with data passed in 'Block'.

             INPUT:  achBlock : the block of data to take the check sum of
            OUTPUT:  none

           RETURNS:  number of bytes loaded into the output array
*/
void VMMD5Checksum::Transform( char achBlock[ 64 ] )
{
  //initialise local data with current checksum
  //
  unsigned long a = m_aulMD5CheckSum[ 0 ];
  unsigned long b = m_aulMD5CheckSum[ 1 ];
  unsigned long c = m_aulMD5CheckSum[ 2 ];
  unsigned long d = m_aulMD5CheckSum[ 3 ];

  //copy BYTES from input 'Block' to an array of unsigned longS 'X'
  //
  unsigned long X[ 16 ];

  CharToDWord( X, achBlock, 64 );

  //Perform Round 1 of the transformation
  //
  FF( a, b, c, d, X[  0 ], VM_MD5_S11, VM_MD5_T01 ); 
  FF( d, a, b, c, X[  1 ], VM_MD5_S12, VM_MD5_T02 ); 
  FF( c, d, a, b, X[  2 ], VM_MD5_S13, VM_MD5_T03 ); 
  FF( b, c, d, a, X[  3 ], VM_MD5_S14, VM_MD5_T04 ); 
  FF( a, b, c, d, X[  4 ], VM_MD5_S11, VM_MD5_T05 ); 
  FF( d, a, b, c, X[  5 ], VM_MD5_S12, VM_MD5_T06 ); 
  FF( c, d, a, b, X[  6 ], VM_MD5_S13, VM_MD5_T07 ); 
  FF( b, c, d, a, X[  7 ], VM_MD5_S14, VM_MD5_T08 ); 
  FF( a, b, c, d, X[  8 ], VM_MD5_S11, VM_MD5_T09 ); 
  FF( d, a, b, c, X[  9 ], VM_MD5_S12, VM_MD5_T10 ); 
  FF( c, d, a, b, X[ 10 ], VM_MD5_S13, VM_MD5_T11 ); 
  FF( b, c, d, a, X[ 11 ], VM_MD5_S14, VM_MD5_T12 ); 
  FF( a, b, c, d, X[ 12 ], VM_MD5_S11, VM_MD5_T13 ); 
  FF( d, a, b, c, X[ 13 ], VM_MD5_S12, VM_MD5_T14 ); 
  FF( c, d, a, b, X[ 14 ], VM_MD5_S13, VM_MD5_T15 ); 
  FF( b, c, d, a, X[ 15 ], VM_MD5_S14, VM_MD5_T16 ); 

  // Perform Round 2 of the transformation
  //
  GG( a, b, c, d, X[  1 ], VM_MD5_S21, VM_MD5_T17 ); 
  GG( d, a, b, c, X[  6 ], VM_MD5_S22, VM_MD5_T18 ); 
  GG( c, d, a, b, X[ 11 ], VM_MD5_S23, VM_MD5_T19 ); 
  GG( b, c, d, a, X[  0 ], VM_MD5_S24, VM_MD5_T20 ); 
  GG( a, b, c, d, X[  5 ], VM_MD5_S21, VM_MD5_T21 ); 
  GG( d, a, b, c, X[ 10 ], VM_MD5_S22, VM_MD5_T22 ); 
  GG( c, d, a, b, X[ 15 ], VM_MD5_S23, VM_MD5_T23 ); 
  GG( b, c, d, a, X[  4 ], VM_MD5_S24, VM_MD5_T24 ); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -