MD5 加密演算法 c語言

2021-06-06 21:58:53 字數 4562 閱讀 9591

摘自openssl

global.h

#ifndef prototypes

#define prototypes 0

#endif

/* pointer defines a generic pointer type */

typedef unsigned char *pointer;

/* uint2 defines a two byte word */

typedef unsigned short int uint2;

/* uint4 defines a four byte word */

typedef unsigned long int uint4;

/* proto_list is defined depending on how prototypes is defined above.

if using prototypes, then proto_list returns the list, otherwise it

returns an empty list.

*/#if prototypes

#define proto_list(list) list

#else

#define proto_list(list) ()

#endif

md5.h

/* md5 context. */

typedef struct md5_ctx;

void md5init proto_list ((md5_ctx *));

void md5update proto_list

((md5_ctx *, unsigned char *, unsigned int));

void md5final proto_list ((unsigned char [16], md5_ctx *));

md5c.c

#include "global.h"

#include "md5.h"

/* constants for md5transform routine.

*/#define s11 7

#define s12 12

#define s13 17

#define s14 22

#define s21 5

#define s22 9

#define s23 14

#define s24 20

#define s31 4

#define s32 11

#define s33 16

#define s34 23

#define s41 6

#define s42 10

#define s43 15

#define s44 21

static void md5transform proto_list ((uint4 [4], unsigned char [64]));

static void encode proto_list

((unsigned char *, uint4 *, unsigned int));

static void decode proto_list

((uint4 *, unsigned char *, unsigned int));

static void md5_memcpy proto_list ((pointer, pointer, unsigned int));

static void md5_memset proto_list ((pointer, int, unsigned int));

static unsigned char padding[64] = ;

/* f, g, h and i are basic md5 functions.

*/#define f(x, y, z) (((x) & (y)) | ((~x) & (z)))

#define g(x, y, z) (((x) & (z)) | ((y) & (~z)))

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

#define i(x, y, z) ((y) ^ ((x) | (~z)))

/* rotate_left rotates x left n bits.

*/#define rotate_left(x, n) (((x) << (n)) | ((x) >> (32-(n))))

/* ff, gg, hh, and ii transformations for rounds 1, 2, 3, and 4.

rotation is separate from addition to prevent recomputation.

*/#define ff(a, b, c, d, x, s, ac)

#define gg(a, b, c, d, x, s, ac)

#define hh(a, b, c, d, x, s, ac)

#define ii(a, b, c, d, x, s, ac)

/* md5 initialization. begins an md5 operation, writing a new context.

*/void md5init (md5_ctx *context)

/* context */

/* md5 block update operation. continues an md5 message-digest

operation, processing another message block, and updating the

context.

*/void md5update (md5_ctx *context, unsigned char *input, unsigned int inputlen)

/* length of input block */

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 md5final (unsigned char digest[16], md5_ctx *context)

/* context */

/* md5 basic transformation. transforms state based on block.

*/static void md5transform (uint4 state[4], unsigned char block[64])

/* encodes input (uint4) into output (unsigned char). assumes len is

a multiple of 4.

*/static void encode (unsigned char *output, uint4 *input, unsigned int len) }

/* decodes input (unsigned char) into output (uint4). assumes len is

a multiple of 4.

*/static void decode (uint4 *output, unsigned char *input, unsigned int len)

/* note: replace "for loop" with standard memcpy if possible.

*/static void md5_memcpy (pointer output, pointer input,unsigned int len)

/* note: replace "for loop" with standard memset if possible.

*/static void md5_memset (pointer output, int value, unsigned int len)

md5_example.c

/* 

md5_example.c: an simply example for routines in md5c.c

author: sh yunchen. [email protected]. 1999.3.31

*/#include "global.h"

#include "md5.h"

#define md5encodelen 16

unsigned char* md5(char*str)

int main(int argc, char*argv)

md5加密演算法

md5.h ifndef md5h define md5h include include void rol unsigned int s,unsigned short cx 32位數迴圈左移實現函式 void ltob unsigned int i b l互轉,接受uint型別 unsigned ...

MD5加密演算法

md5訊息摘要演算法 message digest algorithm 它對輸入的任意長度的訊息進行運算,產生乙個128位的訊息摘要。演算法原理 資料填充 填充訊息使其長度與448模512同餘 長度 448 mod 512 即時訊息長度本身已經滿足了上述長度要求也需要填充。填充方法 附乙個1在訊息後...

加密演算法 MD5

一 簡介 md5的全稱是message digest algorithm 5 資訊摘要演算法 在90年代初由mit laboratory for computer science和rsa data security inc的ronald l.rivest開發出來,經md2 md3和md4發展而來。訊...