Link to home
Start Free TrialLog in
Avatar of David C
David CFlag for United Kingdom of Great Britain and Northern Ireland

asked on

FormsAuthentication.HashPasswordForStoringInConfigFile for VB

Hi Experts, I have an ASP.NET application that I am using to create hashes when registering a user. On the other hand I have a VB.NET application that is suppose to check this hash to authenticate the user. What is the VB.NET Windows Application equivalent of FormsAuthentication.HashPasswordForStoringInConfigFile please?

This is how I am generating the hash

Dim hash As String
        hash = FormsAuthentication.HashPasswordForStoringInConfigFile(Label1.Text & "salt", "SHA1")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you prefer function:
Private Function HashPasswordForStoringInConfigFile(password As String, passwordFormat As String) As String
    Dim ha As System.Security.Cryptography.HashAlgorithm
    If passwordFormat.Equals("sha1", StringComparison.OrdinalIgnoreCase) Then
        ha = Security.Cryptography.SHA1.Create
    ElseIf passwordFormat.Equals("md5", StringComparison.OrdinalIgnoreCase) Then
        ha = Security.Cryptography.MD5.Create
    Else
        Throw New ArgumentException("Invalid password format")
    End If
    Return String.Join("", ha.ComputeHash(
                 System.Text.Encoding.UTF8.GetBytes(password)).
                 Select(Function(x) x.ToString("X2")))
End Function

Open in new window

Avatar of David C

ASKER

Great! First solution works just fine although I  think the second is missing the salt?
Second solution is a copy of FormsAuthentication.HashPasswordForStoringInConfigFile function. You can call it in the same way:
Dim hash As String
hash = HashPasswordForStoringInConfigFile(Label1.Text & "salt", "SHA1")

Open in new window