Explicit type problem in strict mode (Newbie)

A

Andrew Jocelyn

Hi

I'm trying the write code with Strict turned on. I Need to choose a type for
the cryptPassword variable but can't work out what it should be or how I
should convert it. I thought it should be a byte but the ComputeHash is a
Byte array. Can someone tell me what this should be? The code comes from
Wrox ASP.NET Website Programming Prob-Design-Solution book. Some good hints
on how to solve these sorts of problems would be excelent if anyone has any.

Public Shared Function EncryptPassword(ByVal password As String) As Byte()
Dim encoding As New UnicodeEncoding
Dim hashBytes As Byte() = encoding.GetBytes(password)
' Compute the SHA-1 hash
Dim sha1 As New SHA1CryptoServiceProvider
Dim cryptPassword = sha1.ComputeHash(hashBytes)
Return cryptPassword
End Function


Thanks
Andrew
 
G

Greg Burns

Option Strict On

Public Shared Function EncryptPassword(ByVal password As String) As String
Dim encoding As New UnicodeEncoding
Dim hashBytes As Byte() = encoding.GetBytes(password)
' Compute the SHA-1 hash
Dim sha1 As New SHA1CryptoServiceProvider
Dim cryptPassword() As Byte = sha1.ComputeHash(hashBytes)
Return Convert.ToBase64String(cryptPassword)
End Function

(Note: I changed your return type to string. That is usually what you want
to store in your db.)
Some good hints on how to solve these sorts of problems would be excelent
if anyone has any.

Those are definately tricky types to work with. My only suggestion is
pressing F1 when cursor on ComputeHash to see what types the docs say its
return type should be.


Here is some code if you want salt with that hash. :^)

*** Code to get values to store in db...

Dim salt As String = Class1.CreateSalt(5) ' <-character size of salt
Dim passwordHash As String = Class1.CreatePasswordHash(password, salt)

Option Strict On
Imports System.Security.Cryptography
Imports System.Web.Security
Class Class1
Public Shared Function CreateSalt(ByVal size As Integer) As String

' Generate a cryptographic random number using the cryptographic
' service provider
Dim rng As RNGCryptoServiceProvider = New
RNGCryptoServiceProvider
Dim buff As Byte() = New Byte(size) {}
rng.GetBytes(buff)

' Return a Base64 string representation of the random number
Return Convert.ToBase64String(buff)
End Function

Public Shared Function CreatePasswordHash(ByVal pwd As String, ByVal
salt As String) As String
Dim saltAndPwd As String = String.Concat(pwd, salt)
Dim hashedPwd As String =
FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "SHA1")
Return hashedPwd
End Function
End Class


**** code to check entered password against db values for specific
username...


Dim dbPasswordHash As String =
cmd.Parameters("@PasswordHash").Value.ToString
Dim salt As String = cmd.Parameters("@Salt").Value.ToString

' Now take the salt and the password entered by the user
' and concatenate them together.
Dim passwordAndSalt As String = String.Concat(suppliedPassword,
salt)
' Now hash them
Dim hashedPasswordAndSalt As String =
FormsAuthentication.HashPasswordForStoringInConfigFile(passwordAndSalt,
"SHA1")
' Now verify them.
passwordMatch = hashedPasswordAndSalt.Equals(dbPasswordHash)

' if password invalid return 0
If Not passwordMatch Then Return 0

Have fun!
Greg
 
K

Ken Tucker [MVP]

Hi,

Public Shared Function EncryptPassword(ByVal password As String) As Byte()

Dim encoding As New UnicodeEncoding

Dim hashBytes As Byte() = encoding.GetBytes(password)

' Compute the SHA-1 hash

Dim sha1 As New SHA1CryptoServiceProvider

Dim cryptPassword() As Byte = sha1.ComputeHash(hashBytes)

Return cryptPassword

End Function



Another common way to return the encrypted password is to use a base64
string.





Public Shared Function EncryptPassword2(ByVal password As String) As String

Dim encoding As New UnicodeEncoding

Dim hashBytes As Byte() = encoding.GetBytes(password)

' Compute the SHA-1 hash

Dim sha1 As New SHA1CryptoServiceProvider

Return Convert.ToBase64String(sha1.ComputeHash(hashBytes))

End Function



Ken

---------------------------------

message Hi

I'm trying the write code with Strict turned on. I Need to choose a type for
the cryptPassword variable but can't work out what it should be or how I
should convert it. I thought it should be a byte but the ComputeHash is a
Byte array. Can someone tell me what this should be? The code comes from
Wrox ASP.NET Website Programming Prob-Design-Solution book. Some good hints
on how to solve these sorts of problems would be excelent if anyone has any.

Public Shared Function EncryptPassword(ByVal password As String) As Byte()
Dim encoding As New UnicodeEncoding
Dim hashBytes As Byte() = encoding.GetBytes(password)
' Compute the SHA-1 hash
Dim sha1 As New SHA1CryptoServiceProvider
Dim cryptPassword = sha1.ComputeHash(hashBytes)
Return cryptPassword
End Function


Thanks
Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top