Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class Murmur3
- ' 128 bit output, 64 bit platform version
- Public Shared READ_SIZE As ULong = 16
- Private Shared C1 As ULong = &H87c37b91114253d5UL
- Private Shared C2 As ULong = &H4cf5ad432745937fL
- Private length As ULong
- Private seed As UInteger
- ' if want to start with a seed, create a constructor
- Private h1 As ULong
- Private h2 As ULong
- Private Sub MixBody(k1 As ULong, k2 As ULong)
- h1 = h1 Xor MixKey1(k1)
- h1 = h1.RotateLeft(27)
- h1 += h2
- h1 = h1 * 5 + &H52dce729
- h2 = h2 Xor MixKey2(k2)
- h2 = h2.RotateLeft(31)
- h2 += h1
- h2 = h2 * 5 + &H38495ab5
- End Sub
- Private Shared Function MixKey1(k1 As ULong) As ULong
- k1 *= C1
- k1 = k1.RotateLeft(31)
- k1 *= C2
- Return k1
- End Function
- Private Shared Function MixKey2(k2 As ULong) As ULong
- k2 *= C2
- k2 = k2.RotateLeft(33)
- k2 *= C1
- Return k2
- End Function
- Private Shared Function MixFinal(k As ULong) As ULong
- ' avalanche bits
- k = k Xor k >> 33
- k *= &Hff51afd7ed558ccdUL
- k = k Xor k >> 33
- k *= &Hc4ceb9fe1a85ec53UL
- k = k Xor k >> 33
- Return k
- End Function
- Public Function ComputeHash(bb As Byte()) As Byte()
- ProcessBytes(bb)
- Return Hash
- End Function
- Private Sub ProcessBytes(bb As Byte())
- h1 = seed
- Me.length = 0L
- Dim pos As Integer = 0
- Dim remaining As ULong = CULng(bb.Length)
- ' read 128 bits, 16 bytes, 2 longs in eacy cycle
- While remaining >= READ_SIZE
- Dim k1 As ULong = bb.GetUInt64(pos)
- pos += 8
- Dim k2 As ULong = bb.GetUInt64(pos)
- pos += 8
- length += READ_SIZE
- remaining -= READ_SIZE
- MixBody(k1, k2)
- End While
- ' if the input MOD 16 != 0
- If remaining > 0 Then
- ProcessBytesRemaining(bb, remaining, pos)
- End If
- End Sub
- Private Sub ProcessBytesRemaining(bb As Byte(), remaining As ULong, pos As Integer)
- Dim k1 As ULong = 0
- Dim k2 As ULong = 0
- length += remaining
- ' little endian (x86) processing
- Select Case remaining
- Case 15
- k2 = k2 Xor CULng(bb(pos + 14)) << 48
- ' fall through
- goto case 14
- Case 14
- k2 = k2 Xor CULng(bb(pos + 13)) << 40
- ' fall through
- goto case 13
- Case 13
- k2 = k2 Xor CULng(bb(pos + 12)) << 32
- ' fall through
- goto case 12
- Case 12
- k2 = k2 Xor CULng(bb(pos + 11)) << 24
- ' fall through
- goto case 11
- Case 11
- k2 = k2 Xor CULng(bb(pos + 10)) << 16
- ' fall through
- goto case 10
- Case 10
- k2 = k2 Xor CULng(bb(pos + 9)) << 8
- ' fall through
- goto case 9
- Case 9
- k2 = k2 Xor CULng(bb(pos + 8))
- ' fall through
- goto case 8
- Case 8
- k1 = k1 Xor bb.GetUInt64(pos)
- Exit Select
- Case 7
- k1 = k1 Xor CULng(bb(pos + 6)) << 48
- ' fall through
- goto case 6
- Case 6
- k1 = k1 Xor CULng(bb(pos + 5)) << 40
- ' fall through
- goto case 5
- Case 5
- k1 = k1 Xor CULng(bb(pos + 4)) << 32
- ' fall through
- goto case 4
- Case 4
- k1 = k1 Xor CULng(bb(pos + 3)) << 24
- ' fall through
- goto case 3
- Case 3
- k1 = k1 Xor CULng(bb(pos + 2)) << 16
- ' fall through
- goto case 2
- Case 2
- k1 = k1 Xor CULng(bb(pos + 1)) << 8
- ' fall through
- goto case 1
- Case 1
- k1 = k1 Xor CULng(bb(pos))
- ' fall through
- Exit Select
- Case Else
- Throw New Exception("Something went wrong with remaining bytes calculation.")
- End Select
- h1 = h1 Xor MixKey1(k1)
- h2 = h2 Xor MixKey2(k2)
- End Sub
- Public ReadOnly Property Hash() As Byte()
- Get
- h1 = h1 Xor length
- h2 = h2 Xor length
- h1 += h2
- h2 += h1
- h1 = Murmur3.MixFinal(h1)
- h2 = Murmur3.MixFinal(h2)
- h1 += h2
- h2 += h1
- Dim hash__1 = New Byte(Murmur3.READ_SIZE - 1) {}
- Array.Copy(BitConverter.GetBytes(h1), 0, hash__1, 0, 8)
- Array.Copy(BitConverter.GetBytes(h2), 0, hash__1, 8, 8)
- Return hash__1
- End Get
- End Property
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement