Forum Moderators: open

Message Too Old, No Replies

asp.net sha 256

         

paulanthony

10:58 am on Apr 20, 2006 (gmt 0)

10+ Year Member



Anyone know a function to perform the hashing of a string to this algorithm. I cant find one anywhere! plenty of asp implementations - not many .net.

paulanthony

11:21 am on Apr 20, 2006 (gmt 0)

10+ Year Member



Function Convert2ByteArray(strInput As string) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()
arrChar = strInput.ToCharArray()
Dim arrByte(arrChar.Length - 1) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte(intCounter) = Convert.ToByte(arrChar(intCounter))
Next
Return arrByte
End Function

Public Function EncryptSHA256Managed2(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.SHA256Managed()
Dim hash() as Byte = sha.ComputeHash(bytClearString)
Return Convert.ToBase64String(hash)
End Function

Public Function EncryptSHA256Managed(ByVal ClearString As String) As String
Dim arrHashInput As Byte()
Dim arrHashOutput As Byte()
Dim objSHA As SHA256Managed
objSHA = New SHA256Managed
arrHashInput = Convert2ByteArray(ClearString)
arrHashOutput = objSHA.ComputeHash(arrHashInput)
Return(BitConverter.ToString(arrHashOutput))
End Function

However this doesn't return the correct length of the string (either of these functions). I am expecting a string of 64 characters. The original asp application has a sha 256 function which returns this length? so what am I doing wrong?

mrMister

1:43 pm on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds like your original ASP is returning a hexadecimal string representation of the hash.

So you want to convert each element of your byte array to a hex string...


Dim b As Byte

Dim sHexHash As String

For Each b In arrHashOutput

 sHexHash += Hex(b)

Next