[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

SHA1 hash algorithm output?

Rickard Petzäll

7/8/2002 4:16:00 PM

I tried using the Security.Cryptography.SHA1CryptoServiceProvider class to
hash a passphrase string converted to binary array. The problem is that the
hash code does not match the expected code, as specified in the
specifications
(http://csrc.nist.gov/publications/fips/fips180-1/fi...). It is of
importance that the hash code matches the expected value, since they are to
be verified by a Java program (which has been verified to produce correct
SHA1 output).
What am I doing wrong?

The code listing for the subroutine follows.

Public Shared Function HashStringSHA1(ByVal strEPPpwd As String)
Dim sha1 As New Security.Cryptography.SHA1CryptoServiceProvider()
Dim bytes(str.Length) As Byte, out() As Byte
Dim i As Integer
For i = 0 To str.Length - 1
bytes(i) = CByte(AscW(str.Chars(i)))
Next
Dim b As Byte, s As String
For Each b In sha1.ComputeHash(bytes)
s &= Hex(b) & " "
Next
Return s
'Return System.Convert.ToBase64String(sha1.ComputeHash(bytes))
End Function



/ Rickard Petzäll


2 Answers

Mattias Sjögren

7/9/2002 2:15:00 AM

0

Rickard,

>Dim bytes(str.Length) As Byte

This declares an array with lower bound of 0 and upper bound
str.Length, i.e. it has str.Length + 1 elements, but you're never
setting the last element in the loop. I'm guessing that's what gives
you the unexpected hash value.


>Dim i As Integer
>For i = 0 To str.Length - 1
>bytes(i) = CByte(AscW(str.Chars(i)))
>Next

I suggest you use the appropriate Encoding-derived class from the
System.Text namespace for converting your string to a byte array
instead.


>Dim b As Byte, s As String
>For Each b In sha1.ComputeHash(bytes)
>s &= Hex(b) & " "
>Next

It would be a lot more efficient to use a StringBuilder here, to avoid
the creation of many intermediate string objects.


Mattias

==Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.n...

Rickard Petzäll

7/9/2002 9:23:00 AM

0

Indeed, to quote the MSDN for dim
"Each value in boundlist specifies the upper bound of a dimension, not the
length. The lower bound is always zero, so the subscript for each dimension
can vary from zero through the upper-bound value. "
I guess that I should have checked the documentation. Thanks for the help.

/ Rickard

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> skrev i meddelandet
news:uJzFc1tJCHA.2660@tkmsftngp13...
> Rickard,
>
> >Dim bytes(str.Length) As Byte
>
> This declares an array with lower bound of 0 and upper bound
> str.Length, i.e. it has str.Length + 1 elements, but you're never
> setting the last element in the loop. I'm guessing that's what gives
> you the unexpected hash value.
>
>
> >Dim i As Integer
> >For i = 0 To str.Length - 1
> >bytes(i) = CByte(AscW(str.Chars(i)))
> >Next
>
> I suggest you use the appropriate Encoding-derived class from the
> System.Text namespace for converting your string to a byte array
> instead.
>
>
> >Dim b As Byte, s As String
> >For Each b In sha1.ComputeHash(bytes)
> >s &= Hex(b) & " "
> >Next
>
> It would be a lot more efficient to use a StringBuilder here, to avoid
> the creation of many intermediate string objects.
>
>
> Mattias
>
> ===
> Mattias Sjögren (VB MVP)
> mattias @ mvps.org
> http://www.msjogren.n...