首页
社区
课程
招聘
未解决 计算md4的代码问题
发表于: 2021-1-9 13:41 1757

未解决 计算md4的代码问题

2021-1-9 13:41
1757

#include <stdio.h>

 

#include <stdlib.h>

 

#include <string.h>

 

#define min(a,b) (((a) < (b)) ? (a) : (b))
/**/
/ Global Struct Define Section
/*
/

 

enum {
MD4_BLOCK_SIZE = 64,
MD4_DIGEST_SIZE = 16,
MD4_PAD_SIZE = 56
};

 

typedef struct Md4 {
unsigned int buffLen; / in bytes /
unsigned int loLen; / length in bytes /
unsigned int hiLen; / length in bytes /
unsigned int digest[MD4_DIGEST_SIZE / sizeof(unsigned int)];
unsigned int buffer[MD4_BLOCK_SIZE / sizeof(unsigned int)];
} Md4;

 

int main()
{
unsigned char buffer[] = "abcdefghijklmnopqrstuvwxyz";
unsigned char md4Code[MD4_DIGEST_SIZE ] = {0};
Md4 md4 = 0;
md4 = (struct Md4
)malloc(sizeof(struct Md4));
memset(md4, 0, sizeof(md4));

1
2
3
4
5
6
initMd4(md4);
md4Update(md4, buffer, strlen(buffer));
md4Final(md4, md4Code);
 
free(md4);
return 0;

}

 

void initMd4(Md4* md4)
{
md4->digest[0] = 0x67452301L;
md4->digest[1] = 0xefcdab89L;
md4->digest[2] = 0x98badcfeL;
md4->digest[3] = 0x10325476L;

1
2
3
md4->buffLen = 0;
md4->loLen = 0;
md4->hiLen = 0;

}

 

static void AddLength(Md4 md4, unsigned int len)
{
unsigned int tmp = md4->loLen;
if ((md4->loLen += len) < tmp)
md4->hiLen++; /
carry low to high */
}

 

static unsigned int rotlFixed(unsigned int x, unsigned int y)
{
return y ? _lrotl(x, y) : x;
}

 

static void Transform(Md4* md4)
{

 

#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))

 

#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))

 

#define H(x, y, z) ((x) ^ (y) ^ (z))

1
2
3
4
5
/* Copy context->state[] to working vars  */
unsigned int A = md4->digest[0];
unsigned int B = md4->digest[1];
unsigned int C = md4->digest[2];
unsigned int D = md4->digest[3];

#define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+md4->buffer[k],s);
function(A, B, C, D, 0, 3);
function(D, A, B, C, 1, 7);
function(C, D, A, B, 2, 11);
function(B, C, D, A, 3, 19);
function(A, B, C, D, 4, 3);
function(D, A, B, C, 5, 7);
function(C, D, A, B, 6, 11);
function(B, C, D, A, 7, 19);
function(A, B, C, D, 8, 3);
function(D, A, B, C, 9, 7);
function(C, D, A, B, 10, 11);
function(B, C, D, A, 11, 19);
function(A, B, C, D, 12, 3);
function(D, A, B, C, 13, 7);
function(C, D, A, B, 14, 11);
function(B, C, D, A, 15, 19);

 

#undef function

 

#define function(a,b,c,d,k,s) \
a=rotlFixed(a+G(b,c,d)+md4->buffer[k]+0x5a827999,s);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function(A, B, C, D, 0, 3);
function(D, A, B, C, 4, 5);
function(C, D, A, B, 8, 9);
function(B, C, D, A, 12, 13);
function(A, B, C, D, 1, 3);
function(D, A, B, C, 5, 5);
function(C, D, A, B, 9, 9);
function(B, C, D, A, 13, 13);
function(A, B, C, D, 2, 3);
function(D, A, B, C, 6, 5);
function(C, D, A, B, 10, 9);
function(B, C, D, A, 14, 13);
function(A, B, C, D, 3, 3);
function(D, A, B, C, 7, 5);
function(C, D, A, B, 11, 9);
function(B, C, D, A, 15, 13);

#undef function

 

#define function(a,b,c,d,k,s) \
a=rotlFixed(a+H(b,c,d)+md4->buffer[k]+0x6ed9eba1,s);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function(A, B, C, D, 0, 3);
function(D, A, B, C, 8, 9);
function(C, D, A, B, 4, 11);
function(B, C, D, A, 12, 15);
function(A, B, C, D, 2, 3);
function(D, A, B, C, 10, 9);
function(C, D, A, B, 6, 11);
function(B, C, D, A, 14, 15);
function(A, B, C, D, 1, 3);
function(D, A, B, C, 9, 9);
function(C, D, A, B, 5, 11);
function(B, C, D, A, 13, 15);
function(A, B, C, D, 3, 3);
function(D, A, B, C, 11, 9);
function(C, D, A, B, 7, 11);
function(B, C, D, A, 15, 15);
 
/* Add the working vars back into digest state[]  */
md4->digest[0] += A;
md4->digest[1] += B;
md4->digest[2] += C;
md4->digest[3] += D;

}

 

void md4Update(Md4 md4, const unsigned char data, unsigned int len)
{
/ do block size increments /
unsigned char local = (unsigned char)md4->buffer;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (len) {
    unsigned int add = min(len, MD4_BLOCK_SIZE - md4->buffLen);
    memcpy(&local[md4->buffLen], data, add);
 
    md4->buffLen += add;
    data += add;
    len -= add;
 
    if (md4->buffLen == MD4_BLOCK_SIZE) {
        Transform(md4);
        AddLength(md4, MD4_BLOCK_SIZE);
        md4->buffLen = 0;
    }
}

}

 

void md4Final(Md4 md4, unsigned char hash)
{
unsigned char local = (unsigned char)md4->buffer;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
AddLength(md4, md4->buffLen);               /* before adding pads */
 
local[md4->buffLen++] = 0x80/* add 1 */
 
                               /* pad with zeros */
if (md4->buffLen > MD4_PAD_SIZE) {
    memset(&local[md4->buffLen], 0, MD4_BLOCK_SIZE - md4->buffLen);
    md4->buffLen += MD4_BLOCK_SIZE - md4->buffLen;
 
    Transform(md4);
    md4->buffLen = 0;
}
memset(&local[md4->buffLen], 0, MD4_PAD_SIZE - md4->buffLen);
 
/* put lengths in bits */
md4->hiLen = (md4->loLen >> (8 * sizeof(md4->loLen) - 3)) +
    (md4->hiLen << 3);
md4->loLen = md4->loLen << 3;
 
 
/* ! length ordering dependent on digest endian type ! */
memcpy(&local[MD4_PAD_SIZE], &md4->loLen, sizeof(unsigned int));
memcpy(&local[MD4_PAD_SIZE + sizeof(unsigned int)], &md4->hiLen, sizeof(unsigned int));
 
Transform(md4);
 
memcpy(hash, md4->digest, MD4_DIGEST_SIZE);
 
initMd4(md4);  /* reset state */

}


[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 1540
活跃值: (2807)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2


static unsigned int rotlFixed(unsigned int x, unsigned int y)
{
    return y ? _lrotl(x, y) : x;
}
这句话应该怎么改?


===


mingw32循环左移位的宏怎么写?

或者它本身就有函数可以用吗?

最后于 2021-1-9 14:11 被limee编辑 ,原因:
2021-1-9 13:45
0
雪    币: 8125
活跃值: (3819)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
/* RotateDefs.h -- Rotate functions
2015-03-25 : Igor Pavlov : Public domain */

#ifndef __ROTATE_DEFS_H
#define __ROTATE_DEFS_H

#ifdef _MSC_VER

#include <stdlib.h>

/* don't use _rotl with MINGW. It can insert slow call to function. */
 
/* #if (_MSC_VER >= 1200) */
#pragma intrinsic(_rotl)
#pragma intrinsic(_rotr)
/* #endif */

#define rotlFixed(x, n) _rotl((x), (n))
#define rotrFixed(x, n) _rotr((x), (n))

#else

/* new compilers can translate these macros to fast commands. */

#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n))))

#endif

#endif


2021-1-9 23:24
0
雪    币: 1540
活跃值: (2807)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
arab /*&nbsp;RotateDefs.h&nbsp;--&nbsp;Rotate&nbsp;functions 2015-03-25&nbsp;:&n ...
问题解决了,谢谢!
2021-1-10 12:34
0
游客
登录 | 注册 方可回帖
返回
//