On Tue, 04 Nov 2003 13:09:33 GMT, HamuNaptra wrote:
Hi im trying to get a hash from a string and conert it back to a string
eg. "test" = "098f6bcd4621d373cade4e832627b4f6"
my function:
Private Function GenerateHash(ByVal SourceText As String) As String
Dim Ue As New UnicodeEncoding()
Dim ByteSourceText() As Byte = Ue.GetBytes(SourceText)
Dim Md5 As New MD5CryptoServiceProvider()
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
Return Encoding.ASCII.GetString(ByteHash)
End Function
when i call the function i get this:
"yAWeLsdBn1kOedfxt3S/5g=="
I modified your function as follows and got the result you expected:
Private Function GenerateHash(ByVal SourceText As String) As String
'Dim Ue As New UnicodeEncoding
Dim ByteSourceText() As Byte = Encoding.Default.GetBytes(SourceText)
'Dim ByteSourceText() As Byte = Ue.GetBytes(SourceText)
Dim Md5 As New MD5CryptoServiceProvider
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
'Convert the Byte array to a hex string
Dim sb As New StringBuilder(32)
For x As Integer = 0 To ByteHash.Length - 1
If ByteHash(x) < 16 Then
sb.Append("0")
End If
sb.Append(Convert.ToString(ByteHash(x), 16))
Next
Return sb.ToString
End Function
--
Chris
To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.