📄 md5c_tjbi.c
字号:
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.
*/
static void Decode (output, input, len)
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.
*/
static void MD5_memcpy (output, input, len)
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.
*/
static void MD5_memset (output, value, len)
POINTER output;
int value;
unsigned int len;
{
unsigned int i;
for (i = 0; i < len; i++)
((char *)output)[i] = (char)value;
}
/*
void MD5toString(unsigned char *digest,char* buff) {
unsigned int i;
char *ptr = buff;
for (i = 0; i < 16; i++) {
sprintf (ptr,"%02x", digest[i]);
ptr+=2;
}
}
*/
/* ==================================================================== */
/* */
/* MD5_API 函数:Get_FileMD */
/* */
/* 功能: 从文件中获取消息(message)计算message digest */
/* 输入: filename, 包含消息的文件的文件名 */
/* 输出: digest, 输出message digest的长度为16的字符数组 */
/* 返回0, 成功 */
/* 返回-1, 失败 */
/*======================================================================*/
int Get_FileMD (char *filename,unsigned char digest[16])
{
FILE *file;
MD5_CTX context;
int len;
unsigned char buffer[MDBUFFER_SIZE];
#ifdef DEBUG
assert((NULL!=filename)&&(NULL!=digest));
#endif
#ifdef DEBUG
assert((2==sizeof(UINT2))&&(4==sizeof(UINT4)));
#endif
if ((file = fopen (filename, "rb")) == NULL)
{ fprintf (stderr,"%s can't be opened\n", filename);
return -1;
}
else {
MD5Init (&context);
len = fread (buffer, 1, MDBUFFER_SIZE, file);
while (len!=0)
{
MD5Update (&context, buffer, len);
len=fread (buffer,1,MDBUFFER_SIZE,file);
}
MD5Final (digest, &context);
fclose (file);
return 0;
}
}
void Get_BufferMD (unsigned char *string,unsigned int len,unsigned char digest[16])
{
MD5_CTX context;
#ifdef DEBUG
assert((NULL!=string)&&(NULL!=digest));
#endif
#ifdef DEBUG
assert((sizeof(UINT2)==2)&&(sizeof(UINT4)==4)); /*验证机器的数据类型的长度,不符出错*/
#endif
MD5Init (&context);
MD5Update (&context, string, len);
MD5Final (digest, &context);
}
/*
void WriteToFile(unsigned char digest[16],char * filename)
{
char buff[100];
FILE *file;
MD5toString(digest,buff);
if ((file = fopen (filename, "wb")) == NULL)
{
printf (" can't be opened %s \n", filename);
}
else
{
fwrite (buff,32,1,file);
fclose(file);
}
return;
}
*/
//读MD5文件
int ReadMD5File(char* filename, char* strBase64)
{
char buff[100];
FILE *file;
if ((file = fopen (filename, "rb")) == NULL)
{
printf (" Can not open the file %s \n", filename);
}
else
{
fread (buff,32,1,file);
fclose(file);
}
return;
}
const char szBase64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// Convert a string into base64. It can be a binary string
// The length returned in pdwLen a multiple of 4
int ToBase64(unsigned char* inStr, unsigned char* outStr, const int dwLen, int *pdwLen)
{
if (dwLen == 0) return -1;
int j;
int i;
//printf("............Before the md5, the string is : \n");
//for (i=0; i< dwLen; i++)
//{
// printf("the [%d] char is :[%d] [%x]\n", i, inStr[i], inStr[i]);
//}
j = 0;
for (i = 0; i < (int)(dwLen - (dwLen % 3)); i+=3) // Encode 3 bytes at a time.
{
outStr[j] = szBase64[ (inStr[i] & 0xfc) >> 2 ];
//printf("i:%d: [%x], j:%d: [%x]\n", i, inStr[i], j, outStr[j]);
outStr[j+1] = szBase64[ ((inStr[i] & 0x03) << 4) | ((inStr[i+1] & 0xf0) >> 4) ];
outStr[j+2] = szBase64[ ((inStr[i+1] & 0x0f) << 2) | ((inStr[i+2] & 0xc0) >> 6) ];
outStr[j+3] = szBase64[ (inStr[i+2] & 0x3f) ];
j += 4;
}
i = dwLen - (dwLen % 3); // Where we left off before.
switch (dwLen % 3)
{
case 2: // One character padding needed.
{
outStr[j] = szBase64[ (inStr[i] & 0xfc) >> 2 ];
outStr[j+1] = szBase64[ ((inStr[i] & 0x03) << 4) | ((inStr[i+1] & 0xf0) >> 4) ];
outStr[j+2] = szBase64[ (inStr[i+1] & 0x0f) << 2 ];
outStr[j+3] = szBase64[64]; // Pad
outStr[j+4] = '\0';
*pdwLen = j+4;
break;
}
case 1: // Two character padding needed.
{
outStr[j] = szBase64[ (inStr[i] & 0xfc) >> 2 ];
outStr[j+1] = szBase64[ (inStr[i] & 0x03) << 4 ];
outStr[j+2] = szBase64[64]; // Pad
outStr[j+3] = szBase64[64]; // Pad
outStr[j+4] = '\0';
*pdwLen = j+4;
break;
}
case 0:
outStr[j] = '\0';
*pdwLen = j;
break;
}
//printf("............After the md5, the string is : \n");
//for (i=0; i< *pdwLen; i++)
//{
// printf("the [%d] char is : [%d] [%x]\n", i, outStr[i], outStr[i]);
// //outStr++;
//}
return 0;
}
char szDecodeBase64 [] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62, 0, 0, 0,63, // 2F
52,53,54,55,56,57,58,59,60,61, 0, 0, 0,64, 0, 0, // 3F
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, // 4F
15,16,17,18,19,20,21,22,23,24,25, 0, 0, 0, 0, 0, // 5F
0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, // 6F
41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0, // 7F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // AF
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // BF
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // CF
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // DF
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // EF
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // FF
};
#define DECODE(i) ( inStr[i] == '=' ? 0 : szDecodeBase64[inStr[i]] )
// Convert a base64 string into a string. The converted string can be binary.
// The length returned in pdwLen a multiple of 3
int FromBase64(unsigned char* inStr, unsigned char* outStr, int dwLen, int *pdwLen)
{
if (dwLen == 0) return -1;
//unsigned char* lpDecode;
int i;
int j;
//lpDecode = (unsigned char*)LocalAlloc(GPTR, dwLen);
j = 0;
for (i = 0; i < (int)dwLen; i += 4) // Work on 4 bytes at a time.
{
outStr[j++] = (DECODE(i) << 2) | ((DECODE(i+1) & 0x30) >> 4);
if (inStr[i] == '=' || inStr[i+1] == '=' || (unsigned int)i + 1 >= dwLen) break;
outStr[j++] = ((DECODE(i+1) & 0x0f) << 4) | ((DECODE(i+2) & 0x3c) >> 2);
if (inStr[i+1] == '=' || inStr[i+2] == '=' || (unsigned int)i + 2 >= dwLen) break;
outStr[j++] = ((DECODE(i+2) & 0x03) << 6) | (DECODE(i+3) & 0x3f);
if (inStr[i+2] == '=' || inStr[i+3] == '=' || (unsigned int)i + 3 >= dwLen) break;
}
/*
if (i <= dwLen && inStr[i] == '=') dwLen = i;
else if (inStr[i+1] == '=') dwLen = i+1;
else if (inStr[i+2] == '=') dwLen = i+2;
else if (inStr[i+3] == '=') dwLen = i+3;
*/
outStr[j] = '\0';
*pdwLen = j;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -