.NET String Hashing: The Hidden Knot
page 4 of 5
by Joseph Chahine
Feedback
Average Rating: 
Views (Total / Last 10 Days): 23242/ 38

Solution

The ComputeHash function's return value is an array of bytes. In .NET 1.1, the most significant bit of each byte is disregarded when the Encoding.ASCII.GetString function is called. But this is not true in .NET 2.0. So, to simulate the behavior of Encoding.ASCII.GetString as it worked in .NET 1.1, I had to get rid of the most significant bit manually in the .NET 2.0 code by ANDing every byte with the binary value of 01111111 (127 in decimal).

The code becomes as follows:

Listing 2

Function ComputeHashValue(ByVal Data As StringAs String
  Dim HashAlgorithm As SHA1 = SHA1.Create
  Dim HashValue() As Byte =
   HashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(Data))
 
  REM: Looping over the array and ANDing each byte with 0111111
  For i As Integer = 0 To HashValue.Length - 1
    HashValue(i) = HashValue(i) And Convert.ToByte(127)
  Next
  Return Encoding.ASCII.GetString(HashValue)
End Function

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 4:44:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search