Calculate MD5 fastest code

xiaoxiao2021-03-06  15

/ *

Author: crazyd

Date: 2002-12-29

Explanation: This code is written in embedded compilation, and the most core part of MD5 is written.

Have a high computation speed, twice more than C code,

How to use is three functions of MD5init, MD5Update, MD5Final, followed

As of the following standard code:

Standard code:

#include "md5.h"

void main ()

{

MD5_CTX Context;

MD5Init; & context);

MD5UPDATE (& Context, "Crazyd", Strlen ("Crazyd"));

MD5Final (& context);

}

Special Instructions:

Context.State [0] is 32-bit unsigned integers, the back array element is like this,

They are especially paying attention to the direction of storage and display of memory.

* /

#include

Typedef struct {

DWORD State [4]; // Encypted Message

DWord Count [2]; // bits of plaintext

UNSIGNED Char Buffer [64];

} MD5_CTX;

Void MD5INIT (MD5_CTX *);

Void Md5Update (MD5_CTX *, unsigned char *, unsigned int); // The plain text to be encrypted is the intermediate parameter

Void Md5Final (MD5_CTX *);

#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

#define a ESI

#define b Edi

#define c edx

#define d ebx

#define TMP1 EAX

#define TMP2 ECX

#define x (i) [x 4 * i]

Static void Md5Transform (DWORD [4], UNSIGNED CHAR [64]);

Static unsigned charphar padding [64] = {

0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

}

#define FF (A, B, C, D, X, S, AC) /

__ASM MOV TMP1, B /

__ASM and TMP1, C /

__ASM MOV TMP2, B /

__ASM NOT TMP2 /

__ASM and TMP2, D /

__ASM or TMP2, TMP1 /

__ASM Lea A, [TMP2 A AC] /

__ASM ADD A, X /

__ASM ROL A, S / S /

__ASM ADD A, B /

#define GG (A, B, C, D, X, S, AC) / __ ASM MOV TMP1, B /

__ASM and TMP1, D /

__ASM MOV TMP2, D /

__ASM NOT TMP2 /

__ASM and TMP2, C /

__ASM or TMP2, TMP1 /

__ASM Lea A, [TMP2 A AC] /

__ASM ADD A, X /

__ASM ROL A, S / S /

__ASM ADD A, B

#define HH (A, B, C, D, X, S, AC) /

__ASM MOV TMP2, B /

__ASM XOR TMP2, C /

__ASM XOR TMP2, D /

__ASM Lea A, [TMP2 A AC] /

__ASM ADD A, X /

__ASM ROL A, S / S /

__ASM ADD A, B

#define II (A, B, C, D, X, S, AC) /

__ASM MOV TMP2, D /

__ASM NOT TMP2 /

__ASM or TMP2, B /

__ASM XOR TMP2, C /

__ASM Lea A, [TMP2 A AC] /

__ASM ADD A, X /

__ASM ROL A, S / S /

__ASM ADD A, B

Void MD5init (MD5_CTX * Context)

{

CONTEXT-> count [0] = context-> count [1] = 0;

Context-> State [0] = 0x67452301;

Context-> State [1] = 0xEfcdAb89;

CONTEXT-> State [2] = 0x98badcfe;

Context-> State [3] = 0x10325476;

}

Void Md5Update (MD5_CTX * Context, unsigned char * input, unsigned int inputlen)

{

Unsigned Int I, Index, Partle;

Index = (unsigned int) ((CONTEXT-> COUNT [0] >> 3) & 0x3F);

IF ((Context-> Count [0] = (DWORD) INPUTLEN << 3)) <(DWORD) INPUTLEN << 3))

CONTEXT-> COUNT [1] ;

Context-> count [1] = ((DWORD) INPUTLEN >> 29);

Partlen = 64 - INDEX;

IF (Inputlen> = Partle) {

CopyMemory (& context-> buffer);

MD5Transform (context-> state, context-> buffer);

For (i = Partle; i 63

INDEX = 0;

}

Else

i = 0;

/ * Buffer remaining input * /

CopyMemory (& Context-> Buffer [Index], & Input [i], inputlen-i);

}

Void Md5Final (MD5_CTX * Context)

{

UNSIGNED Char Bits [8];

Unsigned int index, padlen

CopyMemory (Bits, Context-> Count, 8); Index = (unsigned int) ((CONTEXT-> COUNT [0] >> 3) & 0x3f);

Padlen = (INDEX <56)? (56 - INDEX): (120 - index);

MD5UPDATE (Context, Padding, Padlen);

MD5UPDATE (Context, Bits, 8);

}

Static Void Md5Transform (DWord State [4], UNSIGNED Char Block [64])

{

DWORD X [16];

__ASM {

// initial

Mov A, 0x67452301

MOV B, 0xEfcdab89

MOV C, 0x98Badcfe

Mov d, 0x10325476

// Copy String from Block To State

// Considering that the API will affect the register, so you can realize this memory copy.

PUSH ESI

Push EDI

XOR ECX, ECX

Mov ESI, DWORD PTR [Block]

Lea edi, [x]

Roll:

MOV Eax, DWORD PTR [ESI ECX]

MOV DWORD PTR [EDI ECX], EAX

Add ECX, 4

CMP ECX, 64

JB Roll

POP EDI

POP ESI

}

/ * ROUND 1 * /

FF (A, B, C, D, X (0), S11, 0xD76AA478); / * 1 * /

FF (D, A, B, C, X (1), S12, 0xE8C7B756); / * 2 * /

FF (C, D, A, B, X (2), S13, 0x242070dB); / * 3 * /

FF (B, C, D, A, X (3), S14, 0XC1BDCEEE); / * 4 * /

FF (A, B, C, D, X (4), S11, 0xF57C0FAF; / * 5 * /

FF (D, A, B, C, X (5), S12, 0x4787C62A); / * 6 * /

FF (C, D, A, B, X (6), S13, 0xA8304613); / * 7 * /

FF (B, C, D, A, X (7), S14, 0xFD469501); / * 8 * /

FF (A, B, C, D, X (8), S11, 0x698098D8); / * 9 * /

FF (D, A, B, C, X (9), S12, 0x8B44F7AF); / * 10 * /

FF (C, D, A, B, X (10), S13, 0xFFFF5BB1); / * 11 * /

FF (B, C, D, A, X (11), S14, 0X895CD7BE); / * 12 * /

FF (A, B, C, D, X (12), S11, 0x6b901122); / * 13 * /

FF (D, A, B, C, X (13), S12, 0xFD987193); / * 14 * /

FF (C, D, A, B, X (14), S13, 0xA679438E); / * 15 * /

FF (B, C, D, A, X (15), S14, 0X49B40821); / * 16 * /

/ * ROUND 2 * /

GG (A, B, C, D, X (1), S21, 0XF61E2562); / * 17 * /

GG (D, A, B, C, X (6), S22, 0XC040B340); / * 18 * /

GG (C, D, A, B, X (11), S23, 0x265E5A51); / * 19 * /

GG (B, C, D, A, X (0), S24, 0xE9B6C7AA); / * 20 * /

GG (A, B, C, D, X (5), S21, 0XD62F105D); / * 21 * / gg (D, A, B, C, X (10), S22, 0X2441453); / * 22 * ​​/

GG (C, D, A, B, X (15), S23, 0xD8A1E681); / * 23 * /

GG (B, C, D, A, X (4), S24, 0xE7D3FBC8); / * 24 * /

GG (A, B, C, D, X (9), S21, 0x21E1CDE6); / * 25 * /

GG (D, A, B, C, X (14), S22, 0XC33707D6); / * 26 * /

GG (C, D, A, B, X (3), S23, 0xF4D50D87); / * 27 * /

GG (B, C, D, A, X (8), S24, 0x455A14ED); / * 28 * /

GG (A, B, C, D, X (13), S21, 0XA9E3E905); / * 29 * /

GG (D, A, B, C, X (2), S22, 0XFCEFA3F8); / * 30 * /

GG (C, D, A, B, X (7), S23, 0x676F02D9); / * 31 * /

GG (B, C, D, A, X (12), S24, 0x8D2A4C8A); / * 32 * /

/ * ROUND 3 * /

HH (A, B, C, D, X (5), S31, 0xFFFFA3942); / * 33 * /

HH (D, A, B, C, X (8), S32, 0x8771F681); / * 34 * /

HH (C, D, A, B, X (11), S33, 0x6D9D6122); / * 35 * /

HH (B, C, D, A, X (14), S34, 0xFDE5380C); / * 36 * /

HH (A, B, C, D, X (1), S31, 0xA4Beea44); / * 37 * /

HH (D, A, B, C, X (4), S32, 0X4BDECFA9); / * 38 * /

HH (C, D, A, B, X (7), S33, 0xF6bb4b60); / * 39 * /

HH (B, C, D, A, X (10), S34, 0XBEBFBC70); / * 40 * /

HH (A, B, C, D, X (13), S31, 0X289B7EC6); / * 41 * /

HH (D, A, B, C, X (0), S32, 0XEAA127FA); / * 42 * /

HH (C, D, A, B, X (3), S33, 0xD4ef3085); / * 43 * /

HH (B, C, D, A, X (6), S34, 0X4881D05); / * 44 * /

HH (A, B, C, D, X (9), S31, 0XD9D4D039); / * 45 * /

HH (D, A, B, C, X (12), S32, 0xE6DB99E5); / * 46 * /

HH (C, D, A, B, X (15), S33, 0x1FA27CF8); / * 47 * /

HH (B, C, D, A, X (2), S34, 0XC4AC5665); / * 48 * /

/ * ROUND 4 * /

II (A, B, C, D, X (0), S41, 0XF4292244); / * 49 * /

II (D, A, B, C, X (7), S42, 0x432AFF97); / * 50 * /

Ii (C, D, A, B, X (14), S43, 0XAB9423A7); / * 51 * /

II (B, C, D, A, X (5), S44, 0XFC93A039); / * 52 * /

II (A, B, C, D, X (12), S41, 0X655B59C3); / * 53 * /

II (D, A, B, C, X (3), S42, 0x8F0CC92); / * 54 * / II (C, D, A, B, X (10), S43, 0xffeff47d); / * 55 * /

II (B, C, D, A, X (1), S44, 0x85845DD1); / * 56 * /

II (A, B, C, D, X (8), S41, 0X6FA87E4F); / * 57 * /

II (D, A, B, C, X (15), S42, 0XFE2CE6E0); / * 58 * /

II (C, D, A, B, X (6), S43, 0XA3014314); / * 59 * /

II (B, C, D, A, X (13), S44, 0X4E0811A1); / * 60 * /

II (A, B, C, D, X (4), S41, 0XF7537E82); / * 61 * /

II (D, A, B, C, X (11), S42, 0xBD3AF235); / * 62 * /

II (C, D, A, B, X (2), S43, 0x2AD7D2BB); / * 63 * /

II (B, C, D, A, X (9), S44, 0XEB86D391); / * 64 * /

__ASM {

MOV TMP1, DWORD PTR [State]

Add DWORD PTR [TMP1], A

Add DWORD PTR [TMP1 4], B

Add DWORD PTR [TMP1 8], C

Add DWORD PTR [TMP1 12], D

}

}

转载请注明原文地址:https://www.9cbs.com/read-46675.html

New Post(0)