根據別人的MD5原始碼封裝的乙個類

2021-04-01 06:19:23 字數 4655 閱讀 9125

//md5.h標頭檔案的內容開始

#pragma once

#define uchar unsigned char

typedef unsigned char *pointer;

typedef unsigned short int uint2;

typedef unsigned long int uint4;

#define proto_list(list) list

//md5 context.

typedef struct md5_ctx 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 *));

/* 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 re***putation.

*/#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)

class cmd5

;//執行所需引數

#define test_block_len 1000

#define test_block_count 1000

static void mdstring proto_list ((char *));

static void mdtimetrial proto_list ((void));

static void mdtestsuite proto_list ((void));

static void mdfile proto_list ((char *));

static void mdfilter proto_list ((void));

static void mdprint proto_list ((unsigned char [16]));

#define md5_ctx md5_ctx

#define mdinit md5init

#define mdupdate md5update

#define mdfinal md5final

/* digests a string and prints the result.

*///md5.h檔案結束

//md5.cap檔案內容開始

#include "stdafx.h"

#include "./md5.h"

cmd5::cmd5(void)

cmd5::~cmd5(void)

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

*/void md5init (md5_ctx *context)

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

operation, processing another message block, and updating the

context.

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

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

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

*/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)

void cmd5::md5transform (uint4 state[4], unsigned char block[64])

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

a multiple of 4.

*/void cmd5::mdstring (uchar *string,uchar *out,int len)

void cmd5::tohex(uchar * out,uchar * lpbuffer,dword dwsize)

;char t[10]=;

for(int i=0;i<(long)dwsize;i++)

memcpy(out,tmp,strlen(tmp));

}//md5.cpp檔案結束

呼叫:cmd5 mymd5;

byte s[256]=;

byte md5[50]=;

mymd5.mdstring(md5,s,sizeof(s));

c語言寫的md5演算法原始碼

1,md5演算法原始碼,輸入是hex string 2.md5演算法原始碼,輸入是檔案 or string 我使用的是輸入是file string.使用方法 md5init是乙個初始化函式,初始化核心變數,裝入標準的幻數 md5update是md5的主計算過程,inbuf是要變換的位元組串,inpu...

封裝Oracle中MD5的用法

為了測試需要,需要在資料庫中直接使用md5,對oracle中md5的用法進行了簡單的封裝,直接在 中執行可以不封裝成函式,如下declare input string varchar2 9999 zbcdefghijklmnopqrstuvwxyz 此處可以修改 raw input raw 128 ...

php 變數的md5加密,PHP中的MD5加密

php中的md5加密 2005 02 17 enet ciweek 綜述 密碼學是研究編制密碼 我們簡稱為加密 encode 和破譯密碼 我們稱之為解密 decode 的技術科學。研究密碼變化的客觀規律,應用於編制密碼以保守通訊秘密的,稱為編碼學 應用於破譯密碼以獲取通訊情報的,稱為破譯學,總稱密碼...