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

📄 ripemd.cpp

📁 计算机安全的 多种密码算法演示
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	Subround(H, a2, b2, c2, d2, e2, X[ 0], 13, k7);
	Subround(H, e2, a2, b2, c2, d2, X[ 4],  7, k7);
	Subround(H, d2, e2, a2, b2, c2, X[13],  5, k7);

	Subround(G, c2, d2, e2, a2, b2, X[ 8], 15, k8);
	Subround(G, b2, c2, d2, e2, a2, X[ 6],  5, k8);
	Subround(G, a2, b2, c2, d2, e2, X[ 4],  8, k8);
	Subround(G, e2, a2, b2, c2, d2, X[ 1], 11, k8);
	Subround(G, d2, e2, a2, b2, c2, X[ 3], 14, k8);
	Subround(G, c2, d2, e2, a2, b2, X[11], 14, k8);
	Subround(G, b2, c2, d2, e2, a2, X[15],  6, k8);
	Subround(G, a2, b2, c2, d2, e2, X[ 0], 14, k8);
	Subround(G, e2, a2, b2, c2, d2, X[ 5],  6, k8);
	Subround(G, d2, e2, a2, b2, c2, X[12],  9, k8);
	Subround(G, c2, d2, e2, a2, b2, X[ 2], 12, k8);
	Subround(G, b2, c2, d2, e2, a2, X[13],  9, k8);
	Subround(G, a2, b2, c2, d2, e2, X[ 9], 12, k8);
	Subround(G, e2, a2, b2, c2, d2, X[ 7],  5, k8);
	Subround(G, d2, e2, a2, b2, c2, X[10], 15, k8);
	Subround(G, c2, d2, e2, a2, b2, X[14],  8, k8);

	Subround(F, b2, c2, d2, e2, a2, X[12],  8, k9);
	Subround(F, a2, b2, c2, d2, e2, X[15],  5, k9);
	Subround(F, e2, a2, b2, c2, d2, X[10], 12, k9);
	Subround(F, d2, e2, a2, b2, c2, X[ 4],  9, k9);
	Subround(F, c2, d2, e2, a2, b2, X[ 1], 12, k9);
	Subround(F, b2, c2, d2, e2, a2, X[ 5],  5, k9);
	Subround(F, a2, b2, c2, d2, e2, X[ 8], 14, k9);
	Subround(F, e2, a2, b2, c2, d2, X[ 7],  6, k9);
	Subround(F, d2, e2, a2, b2, c2, X[ 6],  8, k9);
	Subround(F, c2, d2, e2, a2, b2, X[ 2], 13, k9);
	Subround(F, b2, c2, d2, e2, a2, X[13],  6, k9);
	Subround(F, a2, b2, c2, d2, e2, X[14],  5, k9);
	Subround(F, e2, a2, b2, c2, d2, X[ 0], 15, k9);
	Subround(F, d2, e2, a2, b2, c2, X[ 3], 13, k9);
	Subround(F, c2, d2, e2, a2, b2, X[ 9], 11, k9);
	Subround(F, b2, c2, d2, e2, a2, X[11], 11, k9);

	c1 = state[1] + c1 + d2;
	state[1] = state[2] + d1 + e2;
	state[2] = state[3] + e1 + a2;
	state[3] = state[4] + a1 + b2;
	state[4] = state[0] + b1 + c2;
	state[0] = c1;

  /* Zeroize sensitive information.*/
  RIP_memset ((POINTER)X, 0, sizeof (X));
}

/* Encodes input (UINT4) into output (unsigned char). Assumes len is
  a multiple of 4.
 */
void CRIPEMD::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 CRIPEMD::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 CRIPEMD::RIP_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 CRIPEMD::RIP_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* CRIPEMD::RIPString (char *string)
{
  RIP_CTX context;
  unsigned char digest[16];
  char output1[33];
  static  char output[33]={"\0"};
  unsigned int len = strlen (string);
  int i;
  RIPInit (&context);
  RIPUpdate (&context, (unsigned char*)string, len);
  RIPFinal (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* RIPEMD::RIPFile (CString filename)
CString CRIPEMD::RIPFile (CString filename)
{ 
	static char output[33]={"\0"};
	CFile file;
	CString result;
	RIP_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 
	{
   		RIPInit (&context);
 		while (len = file.Read (buffer, 1024))//, 1024, file))
   				RIPUpdate (&context, buffer, len);
		RIPFinal (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* CRIPEMD::hmac_rip(char* text,char*  key)
{
        char   digest[16];
        char   output1[32];
        static char output[33]={"\0"};
        RIP_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=MD5(key) */
        if (key_len > 64) 
		{
                RIP_CTX      tctx;

                RIPInit(&tctx);
               RIPUpdate(&tctx,(unsigned char*) key, key_len);
                RIPFinal(tk, &tctx);

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

 

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

    
         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
         */
       RIPInit(&context);                   /* init context for 1st
                                              * pass */
       RIPUpdate(&context, k_ipad, 64);      /* start with inner pad */
       RIPUpdate(&context, (unsigned char*)text, text_len); /* then text of datagram 

*/
        RIPFinal((unsigned char*)digest, &context);          /* finish up 1st pass */
        /*
         * perform outer RIP
         */
        RIPInit(&context);                   /* init context for 2nd
                                              * pass */
        RIPUpdate(&context, k_opad, 64);     /* start with outer pad */
        RIPUpdate(&context,(unsigned char*) digest, 16);     /* then results of 1st
                                              * hash */
       RIPFinal((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;     
}
 

⌨️ 快捷键说明

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