/*
* the HMAC_MD5 transform looks like:
*
* MD5(K XOR opad, MD5(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
*/
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void CMd5A::Encode (unsigned char *output,UINT4 *input,unsigned int len)
{
unsigned int i, j;
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4.
*/
void CMd5A::Decode (UINT4 *output,unsigned char *input,unsigned int len)
{
unsigned int i, j;
/* Note: Replace "for loop" with standard memcpy if possible. */
void CMd5A::MD5_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 CMd5A::MD5_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* CMd5A::MDString (char *string)
{
MD5_CTX context;
unsigned char digest[16];
char output1[33];
static char output[33]={"\0"};
unsigned int len = strlen (string);
int i;
MD5Init (&context);
MD5Update (&context, (unsigned char*)string, len);
MD5Final (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* CMd5A::MDFile (CString filename)
CString CMd5A::MDFile (CString filename)
{
static char output[33]={"\0"};
CFile file;
CString result;
MD5_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
{
MD5Init (&context);
while (len = file.Read (buffer, 1024))//, 1024, file))
MD5Update (&context, buffer, len);
MD5Final (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;
}
}
/* Transform as many times as possible.*/
if (inputLen >= partLen)
{
MD5_memcpy( (POINTER)&context->buffer[index], (POINTER)input, partLen);
MD5Transform (context->state, context->buffer);
for (i = partLen; i + 63 < inputLen; i += 64)
MD5Transform (context->state, &input[i]);
index = 0;
}
else
i = 0;
/* Buffer remaining input */
MD5_memcpy( (POINTER)&context->buffer[index], (POINTER)&input[i],inputLen-i );
}
/*
MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context.
*/
void CMd5A::MD5Final (unsigned char digest[16], MD5_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);
MD5Update (context,(unsigned char*) PADDING, padLen);
MD5Update (context, bits, 8); /* Append length (before padding) */
Encode (digest, context->state, 16); /* Store state in digest */
/* MD5 basic transformation. Transforms state based on block.
*/
void CMd5A::MD5Transform (UINT4 state[4],unsigned char block[64])
{
int i=0;
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];