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

📄 sha1.cpp

📁 计算机安全的 多种密码算法演示
💻 CPP
字号:
// RIPEMD.cpp: implementation of the CMd5A class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SHA1.h"
#include "msp.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSHA::CSHA()
{

}

CSHA::~CSHA()
{

}

void CSHA::SHAInit (SHA_CTX *context)    /* context */
{
  context->count[0] = context->count[1] = 0;
  context->state[0] = 0x67452301;    /* Load magic initialization constants.*/
  context->state[1] = 0xefcdab89;
  context->state[2] = 0x98badcfe;
  context->state[3] = 0x10325476;
  context->state[4] = 0xc3d2e1f0;
}


void CSHA::SHAUpdate (SHA_CTX *context, unsigned char *input,unsigned int inputLen  )
{
  unsigned int i, index, partLen;
  index = (unsigned int)((context->count[0] >> 3) & 0x3F);   /* Compute number of bytes mod 64 */

  if ( (context->count[0] += ( (UINT4)inputLen << 3) ) < ( (UINT4)inputLen << 3 ) )
		context->count[1]++;
  
  context->count[1] += ((UINT4)inputLen >> 29);
  partLen = 64 - index;

  /* Transform as many times as possible.*/
  if (inputLen >= partLen) 
  {
	SHA_memcpy( (POINTER)&context->buffer[index], (POINTER)input, partLen);
	SHATransform (context->state, context->buffer);

	for (i = partLen; i + 63 < inputLen; i += 64)
			SHATransform (context->state, &input[i]);

	index = 0;
  }
  else
		i = 0;
  /* Buffer remaining input */
  SHA_memcpy( (POINTER)&context->buffer[index], (POINTER)&input[i],inputLen-i );
}


void CSHA::SHAFinal (unsigned char digest[16], SHA_CTX *context)
                /* message digest */      /* context */
{
  unsigned char bits[8];
  unsigned int index, padLen;
  
  Encode (bits, context->count, 8); /* Save number of bits */

  /* Pad out to 56 mod 64.*/
  index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  padLen = (index < 56) ? (56 - index) : (120 - index);
  SHAUpdate (context,(unsigned char*) PADDING, padLen);

  SHAUpdate (context, bits, 8);   /* Append length (before padding) */
  Encode (digest, context->state, 16); /* Store state in digest */

  /* Zeroize sensitive information.*/
  SHA_memset ((POINTER)context, 0, sizeof (*context));
}

/* RIP basic transformation. Transforms state based on block.
 */
void CSHA::SHATransform (UINT4 state[4],unsigned char block[64])
{
	int i=0;
	UINT4  A, B, C, D, E,eData[16];
        A = state[0];
        B = state[1];
        C = state[2]; 
        D = state[3];
        E = state[4];

	Decode (eData, block, 64);

    subRound( A, B, C, D, E, f1, K1, eData[  0 ] );
	subRound( E, A, B, C, D, f1, K1, eData[  1 ] );
	subRound( D, E, A, B, C, f1, K1, eData[  2 ] );
	subRound( C, D, E, A, B, f1, K1, eData[  3 ] );
	subRound( B, C, D, E, A, f1, K1, eData[  4 ] );
	subRound( A, B, C, D, E, f1, K1, eData[  5 ] );
	subRound( E, A, B, C, D, f1, K1, eData[  6 ] );
	subRound( D, E, A, B, C, f1, K1, eData[  7 ] );
	subRound( C, D, E, A, B, f1, K1, eData[  8 ] );
	subRound( B, C, D, E, A, f1, K1, eData[  9 ] );
	subRound( A, B, C, D, E, f1, K1, eData[ 10 ] );
	subRound( E, A, B, C, D, f1, K1, eData[ 11 ] );
	subRound( D, E, A, B, C, f1, K1, eData[ 12 ] );
	subRound( C, D, E, A, B, f1, K1, eData[ 13 ] );
	subRound( B, C, D, E, A, f1, K1, eData[ 14 ] );
	subRound( A, B, C, D, E, f1, K1, eData[ 15 ] );
	subRound( E, A, B, C, D, f1, K1, expand( eData, 16 ) );
	subRound( D, E, A, B, C, f1, K1, expand( eData, 17 ) );
	subRound( C, D, E, A, B, f1, K1, expand( eData, 18 ) );
	subRound( B, C, D, E, A, f1, K1, expand( eData, 19 ) );

	subRound( A, B, C, D, E, f2, K2, expand( eData, 20 ) );
	subRound( E, A, B, C, D, f2, K2, expand( eData, 21 ) );
	subRound( D, E, A, B, C, f2, K2, expand( eData, 22 ) );
	subRound( C, D, E, A, B, f2, K2, expand( eData, 23 ) );
	subRound( B, C, D, E, A, f2, K2, expand( eData, 24 ) );
	subRound( A, B, C, D, E, f2, K2, expand( eData, 25 ) );
	subRound( E, A, B, C, D, f2, K2, expand( eData, 26 ) );
	subRound( D, E, A, B, C, f2, K2, expand( eData, 27 ) );
	subRound( C, D, E, A, B, f2, K2, expand( eData, 28 ) );
	subRound( B, C, D, E, A, f2, K2, expand( eData, 29 ) );
	subRound( A, B, C, D, E, f2, K2, expand( eData, 30 ) );
	subRound( E, A, B, C, D, f2, K2, expand( eData, 31 ) );
	subRound( D, E, A, B, C, f2, K2, expand( eData, 32 ) );
	subRound( C, D, E, A, B, f2, K2, expand( eData, 33 ) );
	subRound( B, C, D, E, A, f2, K2, expand( eData, 34 ) );
	subRound( A, B, C, D, E, f2, K2, expand( eData, 35 ) );
	subRound( E, A, B, C, D, f2, K2, expand( eData, 36 ) );
	subRound( D, E, A, B, C, f2, K2, expand( eData, 37 ) );
	subRound( C, D, E, A, B, f2, K2, expand( eData, 38 ) );
	subRound( B, C, D, E, A, f2, K2, expand( eData, 39 ) );

	subRound( A, B, C, D, E, f3, K3, expand( eData, 40 ) );
	subRound( E, A, B, C, D, f3, K3, expand( eData, 41 ) );
	subRound( D, E, A, B, C, f3, K3, expand( eData, 42 ) );
	subRound( C, D, E, A, B, f3, K3, expand( eData, 43 ) );
	subRound( B, C, D, E, A, f3, K3, expand( eData, 44 ) );
	subRound( A, B, C, D, E, f3, K3, expand( eData, 45 ) );
	subRound( E, A, B, C, D, f3, K3, expand( eData, 46 ) );
	subRound( D, E, A, B, C, f3, K3, expand( eData, 47 ) );
	subRound( C, D, E, A, B, f3, K3, expand( eData, 48 ) );
	subRound( B, C, D, E, A, f3, K3, expand( eData, 49 ) );
	subRound( A, B, C, D, E, f3, K3, expand( eData, 50 ) );
	subRound( E, A, B, C, D, f3, K3, expand( eData, 51 ) );
	subRound( D, E, A, B, C, f3, K3, expand( eData, 52 ) );
	subRound( C, D, E, A, B, f3, K3, expand( eData, 53 ) );
	subRound( B, C, D, E, A, f3, K3, expand( eData, 54 ) );
	subRound( A, B, C, D, E, f3, K3, expand( eData, 55 ) );
	subRound( E, A, B, C, D, f3, K3, expand( eData, 56 ) );
	subRound( D, E, A, B, C, f3, K3, expand( eData, 57 ) );
	subRound( C, D, E, A, B, f3, K3, expand( eData, 58 ) );
	subRound( B, C, D, E, A, f3, K3, expand( eData, 59 ) );

	subRound( A, B, C, D, E, f4, K4, expand( eData, 60 ) );
	subRound( E, A, B, C, D, f4, K4, expand( eData, 61 ) );
	subRound( D, E, A, B, C, f4, K4, expand( eData, 62 ) );
	subRound( C, D, E, A, B, f4, K4, expand( eData, 63 ) );
	subRound( B, C, D, E, A, f4, K4, expand( eData, 64 ) );
	subRound( A, B, C, D, E, f4, K4, expand( eData, 65 ) );
	subRound( E, A, B, C, D, f4, K4, expand( eData, 66 ) );
	subRound( D, E, A, B, C, f4, K4, expand( eData, 67 ) );
	subRound( C, D, E, A, B, f4, K4, expand( eData, 68 ) );
	subRound( B, C, D, E, A, f4, K4, expand( eData, 69 ) );
	subRound( A, B, C, D, E, f4, K4, expand( eData, 70 ) );
	subRound( E, A, B, C, D, f4, K4, expand( eData, 71 ) );
	subRound( D, E, A, B, C, f4, K4, expand( eData, 72 ) );
	subRound( C, D, E, A, B, f4, K4, expand( eData, 73 ) );
	subRound( B, C, D, E, A, f4, K4, expand( eData, 74 ) );
	subRound( A, B, C, D, E, f4, K4, expand( eData, 75 ) );
	subRound( E, A, B, C, D, f4, K4, expand( eData, 76 ) );
	subRound( D, E, A, B, C, f4, K4, expand( eData, 77 ) );
	subRound( C, D, E, A, B, f4, K4, expand( eData, 78 ) );
	subRound( B, C, D, E, A, f4, K4, expand( eData, 79 ) );
    
	state[0] += A;
	state[1] += B;
	state[2] += C;
	state[3] += D;
	state[4] += E;

  /* Zeroize sensitive information.*/
  SHA_memset ((POINTER)eData, 0, sizeof(eData));
}

/* Encodes input (UINT4) into output (unsigned char). Assumes len is
  a multiple of 4.
 */
void CSHA::Encode (unsigned char *output,UINT4 *input,unsigned int len)
{
  unsigned int i, j;

  for (i = 0, j = 0; j < len; i++, j += 4) 
  {
	output[j] = (unsigned char)(input[i] & 0xff);
	output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
	output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
	output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  }
}

/* Decodes input (unsigned char) into output (UINT4). Assumes len is
  a multiple of 4.
 */
void CSHA::Decode (UINT4 *output,unsigned char *input,unsigned int len)
{
  unsigned int i, j;

  for (i = 0, j = 0; j < len; i++, j += 4)
	output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
}

/* Note: Replace "for loop" with standard memcpy if possible. */
void CSHA::SHA_memcpy (POINTER output,POINTER input,unsigned int len)
{
  unsigned int i;

  for (i = 0; i < len; i++)
	  output[i] = input[i];
}

/* Note: Replace "for loop" with standard memset if possible. */
void CSHA::SHA_memset (POINTER output,int value,unsigned int len)
{
  unsigned int i;

  for (i = 0; i < len; i++)
	 ((char *)output)[i] = (char)value;
}

/* Digests a string and prints the result. */
char* CSHA::SHAString (char *string)
{
  SHA_CTX context;
  unsigned char digest[16];
  char output1[33];
  static  char output[33]={"\0"};
  unsigned int len = strlen (string);
  int i;
  SHAInit (&context);
  SHAUpdate (&context, (unsigned char*)string, len);
  SHAFinal (digest, &context);

  for (i = 0; i < 16; i++)
  {
	  sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
	  //sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
  }
  for(i=0;i<32;i++)
  output[i]=output1[i];
  return output;
}

  
/* Digests a file and prints the result. */
//char* CSHA::SHAFile (CString filename)
CString CSHA::SHAFile (CString filename)
{ 
	static char output[33]={"\0"};
	CFile file;
	CString result;
	SHA_CTX context;
	int len;
	unsigned char buffer[1024], digest[16];
	int i;
	char output1[33];
	//UINT aaa;
	if (file.Open(filename,CFile::modeRead)==0)
	{
		//printf ("%s can't be opened\n", filename);
		AfxMessageBox("open file error");
		return "";
    }
	else 
	{
   		SHAInit(&context);
 		while (len = file.Read (buffer, 1024))//, 1024, file))
   				SHAUpdate(&context, buffer, len);
		SHAFinal (digest, &context);
		file.Close();
		for (i = 0; i < 16; i++)
		{
			sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
  		//	sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
  		}
        for(i=0;i<32;i++)
			output[i]=output1[i];
		result = output;
        return result;
       }
}

char* CSHA::hmac_sha(char* text,char*  key)
{
        char   digest[16];
        char   output1[32];
        static char output[33]={"\0"};
        SHA_CTX context;
        unsigned char k_ipad[65];    /* inner padding -
                                      * key XORd with ipad
                                      */
        unsigned char k_opad[65];    /* outer padding -
                                      * key XORd with opad
                                      */
        unsigned char tk[16];
        int i;
        int text_len = strlen (text);
        int key_len=strlen(key);
        /* if key is longer than 64 bytes reset it to key=SHA(key) */
        if (key_len > 64) 
		{
                SHA_CTX      tctx;

                SHAInit(&tctx);
               SHAUpdate(&tctx,(unsigned char*) key, key_len);
                SHAFinal(tk, &tctx);

                key = (char*)tk;
                key_len = 16;
        }

        /*
         * the HMAC_RIP transform looks like:
         *
         *RIP(K XOR opad, RIP(K XOR ipad, text))
         *
         * where K is an n byte key
         * ipad is the byte 0x36 repeated 64 times
         * opad is the byte 0x5c repeated 64 times
         * and text is the data being protected
         */

        /* start out by storing key in pads */
        
        /*bzero( k_ipad, sizeof k_ipad);
          bzero( k_opad, sizeof k_opad);
        */

        for(i=0;i<65;i++)
	        k_ipad[i]=(unsigned char)0;
        for(i=0;i<65;i++)
		    k_opad[i]=(unsigned char)0;

        /*bcopy( key, k_ipad, key_len);
          bcopy( key, k_opad, key_len);
         */
         for(i=0;i<key_len;i++)
         {
			k_ipad[i]=(unsigned char)key[i];
			k_opad[i]=(unsigned char)key[i];
         }

        /* XOR key with ipad and opad values */
        for (i=0; i<64; i++) 
		{
                k_ipad[i] ^= 0x36;
                k_opad[i] ^= 0x5c;
        }
        /*
         * perform inner RIP
         */
       SHAInit(&context);                   /* init context for 1st
                                              * pass */
       SHAUpdate(&context, k_ipad, 64);      /* start with inner pad */
       SHAUpdate(&context, (unsigned char*)text, text_len); /* then text of datagram 

*/
        SHAFinal((unsigned char*)digest, &context);          /* finish up 1st pass */
        /*
         * perform outer RIP
         */
        SHAInit(&context);                   /* init context for 2nd
                                              * pass */
        SHAUpdate(&context, k_opad, 64);     /* start with outer pad */
        SHAUpdate(&context,(unsigned char*) digest, 16);     /* then results of 1st
                                              * hash */
       SHAFinal((unsigned char*)digest, &context);          /* finish up 2nd pass */
        for (i = 0; i < 16; i++)
        {
			sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
			sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
        }
        for(i=0;i<32;i++)
			output[i]=output1[i]; 
        return output;     
}
 
/*extern "C" __declspec(dllexport) char* RIPString(char * pointer)
{
	RIPEMD rip;
	return rip.RIPString(pointer);
}

extern "C" __declspec(dllexport) CString RIPFile(CString filename)
{
	RIPEMD rip;
	return rip.RIPFile(filename);
}

extern "C" __declspec(dllexport) char* RIP_hmac(char* text, char* key)
{
	RIPEMD rip;
	return rip.hmac_md5(text,key);
}*/

⌨️ 快捷键说明

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