News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Custom hash algorithm

Started by jpam, September 08, 2010, 02:58:13 PM

Previous topic - Next topic

jpam

just a question about some custom hash algorithm.

i made 2 complete different Proc's
one normal and a version that is using mmx
i should be simple, but this algorithm is driving me nuts @!# 

mybe a asm diehard can point me to the right direction ?
i would be very happy to see what i am doing wrong   

the output from both the proc's gives me 8E244C8179D31E12
but it should be                                      8E245D9679D31E12

source html page with the sample avi file is
http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes

Thanks in advance for anyone who want to look at it.
jpam

HashFormat1   db "%.8X%.8X",0

Calc_Hash proc uses esi ebx edx pFile:dword

LOCAL hFile:dword, fSize:dword, NBR:dword, pMem:dword
LOCAL Result[8]:byte

invoke CreateFile,pFile,GENERIC_ALL,0,0,OPEN_EXISTING,0,0
mov hFile,eax
cmp eax,INVALID_HANDLE_VALUE
jz @Error

invoke GetFileSize,hFile,0
mov fSize,eax
push eax

invoke GlobalAlloc,GPTR,131072
mov pMem,eax

invoke ReadFile,hFile,pMem,65536,addr NBR,NULL
sub fSize,65536
add pMem,65536
invoke SetFilePointer,hFile,fSize,NULL,FILE_BEGIN
invoke ReadFile,hFile,pMem,65536,addr NBR,NULL
invoke CloseHandle,hFile

sub pMem,65536
mov esi,pMem
mov ecx,131072
pop eax
mov edx,eax

@@:
add edx,[esi]
add ebx,[esi+4]
add esi,8
sub ecx,8
jnz @B

push edx
push ebx
invoke wsprintf,addr Result,addr HashFormat1
PrintString Result
pop eax
pop eax
jmp @F

@Error:
szText error7,"CreateFile error"
invoke MessageBox,0,addr error7,addr error7,MB_OK
@@:

invoke GlobalFree,pMem
ret

Calc_Hash endp


HashFormat1   db "%.8X%.8X",0

Calc_Hash proc pFile:dword

LOCAL hFile:dword, NBR:dword
LOCAL fSize:dword,count:dword
LOCAL uint64:qword,tmp64:qword

invoke CreateFile,pFile,GENERIC_ALL,0,0,OPEN_EXISTING,0,0
mov hFile,eax
cmp eax,INVALID_HANDLE_VALUE
jz @Error

invoke GetFileSize,hFile,0
movd mm0,eax
mov fSize,eax
mov count,0

@@:
invoke ReadFile,hFile,addr uint64,8,addr NBR,NULL
paddd mm0,qword ptr [uint64]
inc count
cmp count,8192
jnz @B

sub fSize,65536
invoke SetFilePointer,hFile,fSize,NULL,FILE_BEGIN
mov count,0

@@:
invoke ReadFile,hFile,addr uint64,8,addr NBR,NULL
paddd mm0,qword ptr [uint64]
inc count
cmp count,8192
jnz @B

movd eax,mm0
push eax
punpckhdq mm0,mm0
movd eax,mm0
push eax

invoke wsprintf,addr tmp64,addr HashFormat1
PrintString tmp64
jmp @F

@Error:
szText error8,"CreateFile error"
invoke MessageBox,0,addr error8,addr error8,MB_OK
@@:

invoke CloseHandle,hFile
ret

Calc_Hash endp

ecube

i'd be careful with writing a hash alg, the chances of collisions are very high, and obviously being insecure, not sure if you care about that though.

Tedd

You're not doing a 64-bit sum, you're doing two separate 32-bit sums - you need to account for overflow ('carry'); which is why the lower half is correct, but not the upper.
(Unchecked, but I'm guessing this is what you want to do.)

@@:
add edx,[esi]
adc ebx,[esi+4]    ; <-- add-with-carry
add esi,8
sub ecx,8
jnz @B

No snowflake in an avalanche feels responsible.

jpam

Tedd !!!

thanks for looking at the code !
never cross my mind to use adc, it works flawless now !

:U :U