?
Solved

SHA512 hash question

Posted on 2013-06-13
8
Medium Priority
?
371 Views
Last Modified: 2013-06-14
Hi Experts, I am trying to hash some text to but not getting the same result as expected.

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

Open in new window


The text I am hashing is

764553644921011000000http://www.westernmall.co.uk/test/response.aspxFF8F097D1DE0EFC14DE92F65085DED46A796BDFA900AAD1BE0E86ECE8F5B234052843A0754FE27F07E63A3649FD8121329FBAB4F0D7924557C59122BCD4BF13F

Open in new window


And I am expecting

148182038CB072F341FEDF2B9597B6CBF15A2EB01BD98CA6F890B0F60245281DA5AA970E141C2549C2DCA5626AD6E371920C4B72AA54AE98D48AFFA41C8E8798

Open in new window


But I am getting

XKpoFOELiceDiQQFXTnTixDQqTIl/X32nZdb+lp68fSqUK7CeW83kIj89/QOAWW7m+bYnAzceaCnCr5I7rkzDA==

Open in new window


What could I be doing wrong?
0
Comment
Question by:takwirirar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 1

Author Comment

by:takwirirar
ID: 39244562
Ok just checked again and I need to return a HEX
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 39244591
to convert byte array to hex u can use this:
var hex = BitConverter.ToString(byteArray).Replace("-","");

Open in new window

or
StringBuilder sb = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
{
       sb.AppendFormat("{0:x2}", b)
}

var hex = sb.ToString();

Open in new window

0
 
LVL 1

Author Comment

by:takwirirar
ID: 39244605
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

Open in new window


5CAA6814E10B89C7838904055D39D38B10D0A93225FD7DF69D975BFA5A7AF1F4AA50AEC2796F379088FCF7F40E0165BB9BE6D89C0CDC79A0A70ABE48EEB9330C

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 42

Expert Comment

by:sedgwick
ID: 39244626
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

Open in new window

0
 
LVL 1

Author Comment

by:takwirirar
ID: 39245081
That brings out the same result starting with 5C...
0
 
LVL 1

Author Comment

by:takwirirar
ID: 39247278
I found a site that converts the text fine. What could be the difference?
0
 
LVL 1

Author Comment

by:takwirirar
ID: 39247284
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))

Open in new window

0
 
LVL 1

Author Closing Comment

by:takwirirar
ID: 39247287
Thanks to sedgwick!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question