PowerBASIC Forums
  Source Code
  FNV (Fowler/Noll/Vo) 32-bit hash

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   FNV (Fowler/Noll/Vo) 32-bit hash
Wayne Diamond
Member
posted December 09, 2002 09:00 PM     Click Here to See the Profile for Wayne Diamond     Edit/Delete Message   Reply w/Quote
'FNV 32-bit Hash
'===============
'Ported by Wayne Diamond for the PB Forum, 9th December 2002
'Original source: http://www.isthe.com/chongo/src/fnv/hash_32.c
'See also: http://www.isthe.com/chongo/tech/comp/fnv/index.html
'
'FNV (Fowler/Noll/Vo) is a very simple hash designed for
'high-speed hashing, but with minimal collisions.
'The algorithm pseudo-code is something like this:
' hash = offset_basis
' for each byte_of_data to be hashed
' hash = hash * FNV_prime
' hash = hash xor byte_of_data
' return hash

#COMPILE EXE

FUNCTION FNV32(BYVAL dwOffset AS DWORD, BYVAL dwLen AS DWORD, BYVAL offset_basis AS DWORD) AS DWORD
#REGISTER NONE
! mov esi, dwOffset ;esi = ptr to buffer
! mov ecx, dwLen ;ecx = length of buffer (counter)
! mov eax, offset_basis ;set to 0 for FNV-0, or 2166136261 for FNV-1
! mov edi, &h01000193 ;FNV_32_PRIME = 16777619
! xor ebx, ebx ;ebx = 0
nextbyte:
! mul edi ;eax = eax * FNV_32_PRIME
! mov bl, [esi] ;bl = byte from esi
! xor eax, ebx ;al = al xor bl
! inc esi ;esi = esi + 1 (buffer pos)
! dec ecx ;ecx = ecx - 1 (counter)
! jnz nextbyte ;if ecx is 0, jmp to NextByte
! mov FUNCTION, eax ;else, function = eax
END FUNCTION

FUNCTION PBMAIN() AS LONG
DIM Buffer AS STRING, Hash AS DWORD
Buffer = "chongo <Landon Curt Noll> /\../\" 'FNV test string
'FNV-0 call - only use to test the FNV test string to create the offset_basis value
Hash = FNV32(BYVAL STRPTR(Buffer), BYVAL LEN(Buffer), 0)
'FNV-1 (preferred) call: Hash = FNV32(BYVAL STRPTR(Buffer), BYVAL LEN(Buffer), 2166136261)
IF Hash = 2166136261 THEN
STDOUT "Calculated correctly. The hash is: " & STR$(Hash) & " (&h" & HEX$(Hash,8) & ")"
ELSE
STDOUT "Calculation failed! The hash SHOULD be 2166136261, but I calculated " & STR$(Hash)
END IF
WAITKEY$
END FUNCTION

------------------
The PowerBASIC Crypto Archives - What's mine is yours...

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c