;继 MD5算发汇编代码[着重优化] 之后的MD5 神奇优化(欢迎高手前来指点)
;附件:RadASM 工程的所有文件
;需要注意的是 代码优化了只优化了体积 速度却不一定快
WordToHex MACRO _lValue
mov eax,_lValue
xchg al,ah
rol eax,16
xchg al,ah
EXITM <eax>
ENDM
ABCD STRUCT
dwA DWORD ?
dwB DWORD ?
dwC DWORD ?
dwD DWORD ?
ABCD ends
.const
MD5_A EQU 067452301H
MD5_B EQU 0EFCDAB89H
MD5_C EQU 098BADCFEH
MD5_D EQU 010325476H
.data?
szMd5Hex BYTE 33 DUP (?)
.data
stData_FS DWORD 07,12,17,22
stData_GS DWORD 05,09,14,20
stData_HS DWORD 04,11,16,23
stData_IS DWORD 06,10,15,21
stData_FC DWORD 00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15
stData_GC DWORD 01,06,11,00,05,10,15,04,09,14,03,08,13,02,07,12
stData_HC DWORD 05,08,11,14,01,04,07,10,13,00,03,06,09,12,15,02
stData_IC DWORD 00,07,14,05,12,03,10,01,08,15,06,13,04,11,02,09
stData_FF DWORD 0D76AA478H,0E8C7B756H,0242070DBH,0C1BDCEEEH
DWORD 0F57C0FAFH,04787C62AH,0A8304613H,0FD469501H
DWORD 0698098D8H,08B44F7AFH,0FFFF5BB1H,0895CD7BEH
DWORD 06B901122H,0FD987193H,0A679438EH,049B40821H
stData_GG DWORD 0F61E2562H,0C040B340H,0265E5A51H,0E9B6C7AAH
DWORD 0D62F105DH,002441453H,0D8A1E681H,0E7D3FBC8H
DWORD 021E1CDE6H,0C33707D6H,0F4D50D87H,0455A14EDH
DWORD 0A9E3E905H,0FCEFA3F8H,0676F02D9H,08D2A4C8AH
stData_HH DWORD 0FFFA3942H,08771F681H,06D9D6122H,0FDE5380CH
DWORD 0A4BEEA44H,04BDECFA9H,0F6BB4B60H,0BEBFBC70H
DWORD 0289B7EC6H,0EAA127FAH,0D4EF3085H,004881D05H
DWORD 0D9D4D039H,0E6DB99E5H,01FA27CF8H,0C4AC5665H
stData_II DWORD 0F4292244H,0432AFF97H,0AB9423A7H,0FC93A039H
DWORD 0655B59C3H,08F0CCC92H,0FFEFF47DH,085845DD1H
DWORD 06FA87E4FH,0FE2CE6E0H,0A3014314H,04E0811A1H
DWORD 0F7537E82H,0BD3AF235H,02AD7D2BBH,0EB86D391H
szFmt BYTE "%08x%08x%08x%08x",0
.code
_hmemcpy Proc uses edi esi _Dst,_Src,_Count
mov eax,_Count
mov edi,_Dst
mov esi,_Src
mov ecx,eax
shr ecx,02
cld
rep movsd
mov ecx,eax
and ecx,03
rep movsb
ret
_hmemcpy EndP
_ConvertToWordArray Proc uses ebx edi esi _lpData,_dwLen
LOCAL @dwBytes
mov esi,_lpData
mov ebx,_dwLen
mov eax,ebx
add eax,8
shr eax,6
inc eax
shl eax,4
dec eax
shl eax,2
mov @dwBytes,eax
invoke GetProcessHeap
invoke HeapAlloc,eax,HEAP_ZERO_MEMORY,@dwBytes
mov edi,eax
invoke _hmemcpy,edi,esi,ebx
mov DWORD PTR [edi+ebx],128
mov ecx,@dwBytes
shr ecx,2
mov eax,ebx
shr eax,29
mov DWORD PTR [edi+ecx*4],eax
dec ecx
mov eax,_dwLen
shl eax,3
mov DWORD PTR [edi+ecx*4],eax
mov eax,edi
ret
_ConvertToWordArray endp
_MD5 Proc uses ebx edi esi _lpData,_dwLen
LOCAL @Count
LOCAL @abcd:ABCD
LOCAL @ABCD:ABCD
LOCAL @S
invoke _ConvertToWordArray,_lpData,_dwLen
mov esi,eax
lea edi,@abcd
assume edi:PTR ABCD
mov [edi].dwA,MD5_A
mov [edi].dwB,MD5_B
mov [edi].dwC,MD5_C
mov [edi].dwD,MD5_D
mov eax,_dwLen
add eax,8
shr eax,6
inc eax
shl eax,4
mov @Count,eax
xor edx,edx
.While edx<@Count
mov eax,[edi].dwA
mov @ABCD.dwA,eax
mov eax,[edi].dwB
mov @ABCD.dwB,eax
mov eax,[edi].dwC
mov @ABCD.dwC,eax
mov eax,[edi].dwD
mov @ABCD.dwD,eax
xor ebx,ebx
.While ebx<64
mov ecx,ebx
and ecx,48
mov eax,ebx
and eax,3
mov eax,[offset stData_FS+eax*4+ecx]
mov @S,eax
mov eax,ebx
shr eax,4
.if eax==0 ;_md5_F
mov eax,[edi].dwB
not eax
and eax,[edi].dwD
mov ecx,[edi].dwB
and ecx,[edi].dwC
or eax,ecx
.elseif eax==1 ;_md5_G
mov eax,[edi].dwD
not eax
and eax,[edi].dwC
mov ecx,[edi].dwB
and ecx,[edi].dwD
or eax,ecx
.elseif eax==2 ;_md5_H
mov eax,[edi].dwB
xor eax,[edi].dwC
xor eax,[edi].dwD
.elseif eax==3 ;_md5_I
mov eax,[edi].dwD
not eax
or eax,[edi].dwB
xor eax,[edi].dwC
.endif
mov ecx,[offset stData_FC+ebx*4]
add ecx,edx
add eax,[esi+ecx*4] ;x
add eax,[offset stData_FF+ebx*4] ;ac
add eax,[edi].dwA ;a
mov ecx,@S ;s
rol eax,cl
add eax,[edi].dwB ;b
xchg eax,[edi].dwB
xchg eax,[edi].dwC
xchg eax,[edi].dwD
xchg eax,[edi].dwA
inc ebx
.endw
mov eax,@ABCD.dwA
add [edi].dwA,eax
mov eax,@ABCD.dwB
add [edi].dwB,eax
mov eax,@ABCD.dwC
add [edi].dwC,eax
mov eax,@ABCD.dwD
add [edi].dwD,eax
add edx,16
.endw
mov [edi].dwA,WordToHex([edi].dwA)
mov [edi].dwB,WordToHex([edi].dwB)
mov [edi].dwC,WordToHex([edi].dwC)
mov [edi].dwD,WordToHex([edi].dwD)
invoke wsprintf,offset szMd5Hex,offset szFmt,[edi].dwA,[edi].dwB,[edi].dwC,[edi].dwD
assume edi:Nothing
invoke GetProcessHeap
invoke HeapFree,eax,NULL,esi
lea eax,szMd5Hex
ret
_MD5 endp
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课