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

📄 ripemd.cpp

📁 实现多种加解密算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:

//RIPEMD.cpp
#include "stdafx.h"
#include "RIPEMD.h"

#include <exception>
#include <strstream>

using namespace std;

//CONSTRUCTOR
CRIPEMD::CRIPEMD(int iMethod)
{
	//Check the method
	switch(iMethod)
	{
		case RIPEMD128:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			break;

		case RIPEMD160:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			m_auiBuf[4] = 0xc3d2e1f0;
			break;

/*
		case RIPEMD256:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			m_auiBuf[4] = 0x76543210;
			m_auiBuf[5] = 0xfedcba98;
			m_auiBuf[6] = 0x89abcdef;
			m_auiBuf[7] = 0x01234567;
			break;

		case RIPEMD320:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			m_auiBuf[4] = 0xc3d2e1f0;
			m_auiBuf[5] = 0x76543210;
			m_auiBuf[6] = 0xfedcba98;
			m_auiBuf[7] = 0x89abcdef;
			m_auiBuf[8] = 0x01234567;
			m_auiBuf[8] = 0x3c2d1e0f;
			break;
*/

		default:
		{
			ostrstream ostr;
			ostr << "FileDigest ERROR: in CRIPEMD() Constructor, Illegal Method " << iMethod << "!" << ends;
			string ostrMsg = ostr.str();
			ostr.freeze(false);
			throw runtime_error(ostrMsg);
		}
	}
	m_iMethod = iMethod;
	m_auiBits[0] = 0;
	m_auiBits[1] = 0;
}

//Update context to reflect the concatenation of another buffer of bytes.
void CRIPEMD::AddData(char const* pcData, int iDataLength)
{
	if(iDataLength < 0)
		throw runtime_error(string("FileDigest ERROR: in CRIPEMD::AddData(), Data Length should be >= 0!"));
	unsigned int uiT;
	//Update bitcount
	uiT = m_auiBits[0];
	if((m_auiBits[0] = uiT + ((unsigned int)iDataLength << 3)) < uiT)
		m_auiBits[1]++; //Carry from low to high
	m_auiBits[1] += iDataLength >> 29;
	uiT = (uiT >> 3) & (BLOCKSIZE-1); //Bytes already
	//Handle any leading odd-sized chunks
	if(uiT != 0)
	{
		unsigned char *puc = (unsigned char *)m_aucIn + uiT;
		uiT = BLOCKSIZE - uiT;
		if(iDataLength < uiT)
		{
			memcpy(puc, pcData, iDataLength);
			return;
		}
		memcpy(puc, pcData, uiT);
		Transform();
		pcData += uiT;
		iDataLength -= uiT;
	}
	//Process data in 64-byte chunks
	while(iDataLength >= BLOCKSIZE)
	{
		memcpy(m_aucIn, pcData, BLOCKSIZE);
		Transform();
		pcData += BLOCKSIZE;
		iDataLength -= BLOCKSIZE;
	}
	//Handle any remaining bytes of data
	memcpy(m_aucIn, pcData, iDataLength);
	//Set the flag
	m_bAddData = true;
}

//Final wrapup - pad to 64-byte boundary with the bit pattern 
//1 0*(64-bit count of bits processed, MSB-first)
void CRIPEMD::FinalDigest(char* pcDigest)
{
	//Is the User's responsability to ensure that pcDigest has at least 16 bytes allocated
	if(false == m_bAddData)
		throw runtime_error(string("FileDigest ERROR: in CRIPEMD::FinalDigest(), No data Added before call!"));
	unsigned int uiCount;
	unsigned char *puc;
	//Compute number of bytes mod 64
	uiCount = (m_auiBits[0] >> 3) & (BLOCKSIZE-1);
	//Set the first char of padding to 0x80. This is safe since there is always at least one byte free
	puc = m_aucIn + uiCount;
	*puc++ = 0x80;
	//Bytes of padding needed to make 64 bytes
	uiCount = BLOCKSIZE - uiCount - 1;
	//Pad out to 56 mod 64
	if(uiCount < 8)
	{
		//Two lots of padding: Pad the first block to 64 bytes
		memset(puc, 0, uiCount);
		Transform();
		//Now fill the next block with 56 bytes
		memset(m_aucIn, 0, BLOCKSIZE-8);
	}
	else
	{
		//Pad block to 56 bytes
		memset(puc, 0, uiCount - 8);
	}
	//Append length in bits and transform
	((unsigned int*)m_aucIn)[(BLOCKSIZE>>2)-2] = m_auiBits[0];
	((unsigned int*)m_aucIn)[(BLOCKSIZE>>2)-1] = m_auiBits[1];
	Transform();
	switch(m_iMethod)
	{
		case RIPEMD128:
			memcpy(pcDigest, m_auiBuf, RIPEMD128LENGTH<<2);
			break;

		case RIPEMD160:
			memcpy(pcDigest, m_auiBuf, RIPEMD160LENGTH<<2);
			break;
	}
	//Reinitialize
	Reset();
}

//Reset current operation in order to prepare a new one
void CRIPEMD::Reset()
{
	//Reinitialize
	switch(m_iMethod)
	{
		case RIPEMD128:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			break;

		case RIPEMD160:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			m_auiBuf[4] = 0xc3d2e1f0;
			break;

/*
		case RIPEMD256:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			m_auiBuf[4] = 0x76543210;
			m_auiBuf[5] = 0xfedcba98;
			m_auiBuf[6] = 0x89abcdef;
			m_auiBuf[7] = 0x01234567;
			break;

		case RIPEMD320:
			m_auiBuf[0] = 0x67452301;
			m_auiBuf[1] = 0xefcdab89;
			m_auiBuf[2] = 0x98badcfe;
			m_auiBuf[3] = 0x10325476;
			m_auiBuf[4] = 0xc3d2e1f0;
			m_auiBuf[5] = 0x76543210;
			m_auiBuf[6] = 0xfedcba98;
			m_auiBuf[7] = 0x89abcdef;
			m_auiBuf[8] = 0x01234567;
			m_auiBuf[8] = 0x3c2d1e0f;
			break;
*/

	}
	m_auiBits[0] = 0;
	m_auiBits[1] = 0;
	//Reset the flag
	m_bAddData = false;
}

//The core of the RIPEMD algorithm, this alters an existing RIPEMD hash to
//reflect the addition of 16 longwords of new data.
void CRIPEMD::Transform()
{
	unsigned int* puiIn = (unsigned int*)m_aucIn;
	switch(m_iMethod)
	{
		case RIPEMD128:
			{
				unsigned int aa, bb, cc, dd;
				unsigned int aaa, bbb, ccc, ddd;
				aa = m_auiBuf[0];
				bb = m_auiBuf[1];
				cc = m_auiBuf[2];
				dd = m_auiBuf[3];
				aaa = m_auiBuf[0];
				bbb = m_auiBuf[1];
				ccc = m_auiBuf[2];
				ddd = m_auiBuf[3];
				//Round 1
				FF128(aa, bb, cc, dd, puiIn[ 0], 11);
				FF128(dd, aa, bb, cc, puiIn[ 1], 14);
				FF128(cc, dd, aa, bb, puiIn[ 2], 15);
				FF128(bb, cc, dd, aa, puiIn[ 3], 12);
				FF128(aa, bb, cc, dd, puiIn[ 4],  5);
				FF128(dd, aa, bb, cc, puiIn[ 5],  8);
				FF128(cc, dd, aa, bb, puiIn[ 6],  7);
				FF128(bb, cc, dd, aa, puiIn[ 7],  9);
				FF128(aa, bb, cc, dd, puiIn[ 8], 11);
				FF128(dd, aa, bb, cc, puiIn[ 9], 13);
				FF128(cc, dd, aa, bb, puiIn[10], 14);
				FF128(bb, cc, dd, aa, puiIn[11], 15);
				FF128(aa, bb, cc, dd, puiIn[12],  6);
				FF128(dd, aa, bb, cc, puiIn[13],  7);
				FF128(cc, dd, aa, bb, puiIn[14],  9);
				FF128(bb, cc, dd, aa, puiIn[15],  8);             
				//Round 2
				GG128(aa, bb, cc, dd, puiIn[ 7],  7);
				GG128(dd, aa, bb, cc, puiIn[ 4],  6);
				GG128(cc, dd, aa, bb, puiIn[13],  8);
				GG128(bb, cc, dd, aa, puiIn[ 1], 13);
				GG128(aa, bb, cc, dd, puiIn[10], 11);
				GG128(dd, aa, bb, cc, puiIn[ 6],  9);
				GG128(cc, dd, aa, bb, puiIn[15],  7);
				GG128(bb, cc, dd, aa, puiIn[ 3], 15);
				GG128(aa, bb, cc, dd, puiIn[12],  7);
				GG128(dd, aa, bb, cc, puiIn[ 0], 12);
				GG128(cc, dd, aa, bb, puiIn[ 9], 15);
				GG128(bb, cc, dd, aa, puiIn[ 5],  9);
				GG128(aa, bb, cc, dd, puiIn[ 2], 11);
				GG128(dd, aa, bb, cc, puiIn[14],  7);
				GG128(cc, dd, aa, bb, puiIn[11], 13);
				GG128(bb, cc, dd, aa, puiIn[ 8], 12);
				//Round 3
				HH128(aa, bb, cc, dd, puiIn[ 3], 11);
				HH128(dd, aa, bb, cc, puiIn[10], 13);
				HH128(cc, dd, aa, bb, puiIn[14],  6);
				HH128(bb, cc, dd, aa, puiIn[ 4],  7);
				HH128(aa, bb, cc, dd, puiIn[ 9], 14);
				HH128(dd, aa, bb, cc, puiIn[15],  9);
				HH128(cc, dd, aa, bb, puiIn[ 8], 13);
				HH128(bb, cc, dd, aa, puiIn[ 1], 15);
				HH128(aa, bb, cc, dd, puiIn[ 2], 14);
				HH128(dd, aa, bb, cc, puiIn[ 7],  8);
				HH128(cc, dd, aa, bb, puiIn[ 0], 13);
				HH128(bb, cc, dd, aa, puiIn[ 6],  6);
				HH128(aa, bb, cc, dd, puiIn[13],  5);
				HH128(dd, aa, bb, cc, puiIn[11], 12);
				HH128(cc, dd, aa, bb, puiIn[ 5],  7);
				HH128(bb, cc, dd, aa, puiIn[12],  5);
				//Round 4
				II128(aa, bb, cc, dd, puiIn[ 1], 11);
				II128(dd, aa, bb, cc, puiIn[ 9], 12);
				II128(cc, dd, aa, bb, puiIn[11], 14);
				II128(bb, cc, dd, aa, puiIn[10], 15);
				II128(aa, bb, cc, dd, puiIn[ 0], 14);
				II128(dd, aa, bb, cc, puiIn[ 8], 15);
				II128(cc, dd, aa, bb, puiIn[12],  9);
				II128(bb, cc, dd, aa, puiIn[ 4],  8);
				II128(aa, bb, cc, dd, puiIn[13],  9);
				II128(dd, aa, bb, cc, puiIn[ 3], 14);

⌨️ 快捷键说明

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