David C
asked on
SHA512 hash question
Hi Experts, I am trying to hash some text to but not getting the same result as expected.
THis is my code
The text I am hashing is
And I am expecting
But I am getting
What could I be doing wrong?
THis is my code
Public Function EncryptSHA512Managed(ByVal ClearString As String) As String
Dim uEncode As New UnicodeEncoding()
Dim bytClearString() As Byte = uEncode.GetBytes(ClearString)
Dim sha As New _
System.Security.Cryptography.SHA512Managed()
Dim hash() As Byte = sha.ComputeHash(bytClearString)
Return Convert.ToBase64String(hash)
End Function
The text I am hashing is
764553644921011000000http://www.westernmall.co.uk/test/response.aspxFF8F097D1DE0EFC14DE92F65085DED46A796BDFA900AAD1BE0E86ECE8F5B234052843A0754FE27F07E63A3649FD8121329FBAB4F0D7924557C59122BCD4BF13F
And I am expecting
148182038CB072F341FEDF2B9597B6CBF15A2EB01BD98CA6F890B0F60245281DA5AA970E141C2549C2DCA5626AD6E371920C4B72AA54AE98D48AFFA41C8E8798
But I am getting
XKpoFOELiceDiQQFXTnTixDQqTIl/X32nZdb+lp68fSqUK7CeW83kIj89/QOAWW7m+bYnAzceaCnCr5I7rkzDA==
What could I be doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is the new code but I am now getting
Protected Sub Button4_Click(sender As Object, e As System.EventArgs) Handles Button4.Click
EncryptSHA512Managed("764553644921011000000http://www.westernmall.co.uk/test/response.aspxFF8F097D1DE0EFC14DE92F65085DED46A796BDFA900AAD1BE0E86ECE8F5B234052843A0754FE27F07E63A3649FD8121329FBAB4F0D7924557C59122BCD4BF13F")
End Sub
Public Function EncryptSHA512Managed(ByVal ClearString As String) As String
Dim uEncode As New UnicodeEncoding()
Dim bytClearString() As Byte = uEncode.GetBytes(ClearString)
Dim sha As New _
System.Security.Cryptography.SHA512Managed()
Dim hash() As Byte = sha.ComputeHash(bytClearString)
Dim hex = BitConverter.ToString(hash).Replace("-", "")
Response.Write(hex)
End Function
5CAA6814E10B89C7838904055D39D38B10D0A93225FD7DF69D975BFA5A7AF1F4AA50AEC2796F379088FCF7F40E0165BB9BE6D89C0CDC79A0A70ABE48EEB9330C
try this:
Protected Sub Button4_Click(sender As Object, e As System.EventArgs) Handles Button4.Click
EncryptSHA512Managed("764553644921011000000http://www.westernmall.co.uk/test/response.aspxFF8F097D1DE0EFC14DE92F65085DED46A796BDFA900AAD1BE0E86ECE8F5B234052843A0754FE27F07E63A3649FD8121329FBAB4F0D7924557C59122BCD4BF13F")
End Sub
Public Function EncryptSHA512Managed(ByVal ClearString As String) As String
Dim bytClearString() As Byte = GetBytes(ClearString)
Dim sha As New _
System.Security.Cryptography.SHA512Managed()
Dim hash() As Byte = sha.ComputeHash(bytClearString)
Dim hex = BitConverter.ToString(hash).Replace("-", "")
Response.Write(hex)
End Function
Private Function GetBytes(str As String) As Byte()
Dim bytes As Byte() = New Byte(str.Length * 2 - 1) {}
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length)
Return bytes
End Function
ASKER
That brings out the same result starting with 5C...
ASKER
I found a site that converts the text fine. What could be the difference?
ASKER
Ok I have sussed it. I had to convert the byte to be hashed to UTF8 first
Dim bytClearString() As Byte = Encoding.UTF8.GetBytes((ClearString))
ASKER
Thanks to sedgwick!
ASKER