资源简介
C语言版本Linux环境下MD5加密函数,需要在Linux换进下运行。经测试验证,完全正确,内附带使用说明,谢谢
代码片段和文件信息
#include
#include
#include
#include
#include
/*
* Convert an array of little-endian words to a hex string.
*/
int binl2hex( int *src char *dst int nsize)
{
if( src == 0 )
return 1;
if( dst == 0 )
return 1;
if( nsize < 32 )
return 1;
char hex_tab[] = “0123456789abcdef“;
int j = 0;
int i;
for(i = 0; i < 16; i++)
{
dst[j++] = hex_tab[(src[i>>2] >> ((i%4)*8+4)) & 0xF];
syslog(LOG_DEBUG“end:%c-%d-%d-%d“dst[j-1](src[i>>2] >> ((i%4)*8+4))((i%4)*8+4)src[i>>2]);
dst[j++] = hex_tab[(src[i>>2] >> ((i%4)*8 )) & 0xF];
syslog(LOG_DEBUG“end:%c-%d-%d-%d“dst[j-1](src[i>>2] >> ((i%4)*8+4))((i%4)*8+4)src[i>>2]);
}
dst[j] = 0;
return 0;
}
/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII characters >255 have their hi-byte silently ignored.
*/
int str2binl(char * srcint *pndst int nsize)
{
if(src == 0)
return 1;
if(pndst == 0)
return 1;
int nloop = strlen(src) * 8;
if( (nloop >> 5) >= nsize )
return 1;
int i;
for(i=0; i pndst[i] = 0;
int mask = (1 << 8) - 1;
for(i = 0; i < nloop; i += 8)
{
pndst[i>>5] |= ( src[i / 8] & mask) << (i%32);
}
return 0;
}
/*
* Add integers wrapping at 2^32.
*/
int safe_add(int x int y)
{
int lsw = (x & 0xFFFF) + (y & 0xFFFF);
int msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
/*
* Bitwise rotate a 32-bit number to the left.
*/
int bit_rol(unsigned int num unsigned int cnt)
{
//old
// return (num << cnt) | (num >>> (32 - cnt));
return (num << cnt) | (num >> (32 - cnt));
}
/*
* These functions implement the four basic operations the algorithm uses.
*/
int md5_cmn(int q int a int b int x int s int t)
{
return safe_add(bit_rol(safe_add(safe_add(a q) safe_add(x t)) s)b);
}
int md5_ff( int a int b int c int d int x int s int t)
{
return md5_cmn((b & c) | ((~b) & d) a b x s t);
}
int md5_gg( int a int b int c int d int x int s int t)
{
return md5_cmn((b & d) | (c & (~d)) a b x s t);
}
int md5_hh( int a int b int c int d int x int s int t)
{
return md5_cmn(b ^ c ^ d a b x s t);
}
int md5_ii( int a int b int c int d int x int s int t)
{
return md5_cmn(c ^ (b | (~d)) a b x s t);
}
/*
* Calculate the MD5 of an array of little-endian words and a bit length
*/
int core_md5( int *x int len int * dst)
{
if( x == 0)
return 1;
if( len <= 0 )
return 1;
if (dst == 0)
return 1;
/* append padding */
x[len >> 5] |= 0x80 << ((len) % 32);
syslog(LOG_DEBUG“x[len >> 5]:%d“x[len >> 5]);
//old
//--x[(((len + 64) >>> 9) << 4) + 14] = len;
unsigned int ntemp = len + 64;
x[(((ntemp) >> 9) << 4) + 14] = len;
syslog(LOG_DEBUG“x[(((ntemp) >> 9) << 4) + 14]:%d“x[(((ntemp) >> 9) << 4) + 14]);
int a = 1732584193;
int b = -271733879;
int c = -1732584194;
int d = 27173
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 8493 2013-11-04 17:28 Linux-MD5\Linux-md5.c
文件 452 2013-11-05 18:14 Linux-MD5\method of usage.txt
目录 0 2013-11-05 18:15 Linux-MD5
----------- --------- ---------- ----- ----
8945 3
- 上一篇:rsa MFC实现源码
- 下一篇:C语言库函数手册--c语言大全
评论
共有 条评论