|
Post by Kelly Ethridge on Nov 2, 2006 8:37:13 GMT -5
In order to compute the hash for a string, the string must be converted to a byte array. There are a couple of ways this can be done.
One way is to use the StrConv method. However, since this method returns a String, it must be assigned to a byte array before passing the value into the hash algorithm.
Dim Data() As Byte Data = StrConv("This is my text.", vbFromUnicode)
Dim Hasher As New SHA256Managed Dim Hash() As Byte Hash = Hasher.ComputeHash(Data)
Another method is to use the encoding classes, such as UTF8Encoding. Since the encoding classes return a byte array from a string, no intermediate variable is required.
Dim Hasher As New SHA256Managed Dim Hash() As Byte Hash = Hasher.ComputeHash(Encoding.UTF8.GetBytes("This is my text."))
These two methods may not compute the same hash value for the same string. This is because the conversion from string to byte array may produce a different set of byte values. It is important to use the same encoding when comparing hash values produced by strings.
|
|