Code tagged with xor

Compute 32-bit hash with XOR

The following code computes a hash value for a byte array and returns it as an Int32. This can be faster or more useful than MD5, depending on your needs.

Shared Function ComputeInt32XORHash(ByVal Seed As Int32, _
  ByVal Buffer() As Byte, ByVal Index As Integer, _
  ByVal Length As Integer) As Int32

  Dim HashCode As Int32 = Seed

  While Index <= Length - 4
    HashCode = HashCode Xor BitConverter.ToInt32(Buffer, Index)
    Index += 4
  End While

  If Index < Length Then
    Dim Offset As Integer = 0
    Dim LastBlock(3) As Byte

    For Offset = 0 To Length - Index - 1
      LastBlock(Offset) = Buffer(Index + Offset)
    Next
    HashCode = HashCode Xor BitConverter.ToInt32(LastBlock, 0)
  End If

  Return HashCode
End Function
Language VB.NET / Tagged with xor, hash