Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Structure XorUInt64
- Private Shared Rnd As Random
- Private StoredValue As UInt64
- Private TokenHash ,ValueHash As Byte
- Private Function RndUInt64() As UInt64
- Dim ULongBytes(7) As Byte
- Rnd.NextBytes(ULongBytes)
- Return BitConverter.ToUInt64(ULongBytes, 0)
- End Function
- Private Function Hash(ByVal Token As UInt64) As Byte
- Dim TokenBytes() As Byte = BitConverter.GetBytes(Token)
- Dim HashCode As Int16 = TokenBytes(0)
- HashCode += TokenBytes(1) Xor 170
- HashCode += TokenBytes(2) Xor 170
- HashCode += TokenBytes(3) Xor 170
- HashCode += TokenBytes(4) Xor 170
- HashCode += TokenBytes(5) Xor 170
- HashCode += TokenBytes(6) Xor 170
- HashCode += TokenBytes(7) Xor 170
- Return HashCode Mod 256
- End Function
- Public Sub New(ByRef Token As UInt64)
- Rnd = New Random
- Token = RndUInt64()
- TokenHash = Hash(Token)
- End Sub
- Public Sub Store(ByRef Token As UInt64, ByVal Value As UInt64)
- If Hash(Token) = TokenHash Then
- Token = RndUInt64()
- TokenHash = Hash(Token)
- ValueHash = Hash(Value)
- StoredValue = Token Xor Value
- Else
- Throw New Exception("Token hash mismatch")
- End If
- End Sub
- Default Public ReadOnly Property Value(ByVal Token As UInt64) As UInt64
- Get
- If Hash(Token) <> TokenHash Then Throw New Exception("Token hash mismatch")
- Dim R As UInt64 = StoredValue Xor Token
- If Hash(R) = ValueHash Then Return R Else Throw New Exception("Return value inconsistent with stored value")
- End Get
- End Property
- End Structure
Advertisement
Add Comment
Please, Sign In to add comment