Allahmuad Abu Zhar
Member
So basically, i'm making a login system with vb.net and mysql. It works perfectly when i use plain text. However i wanna make it more secure by hashing the contents using sha512. If i register and hash the credentials, the hash will be added in the database. But when i try to log in, it says invalid credentials.
Here's the code if it helps somehow:
login function:
hash function:
register function:
Edit:
I found that the output hash was to long. It works with sha-1 but not sha512. But i still think it should work with sha512 so if anyone could help me i would appreciate it.
Here's the code if it helps somehow:
login function:
VB.NET:
Dim username As String = hash(usern.Text)
Dim password As String = hash(passw.Text)
Dim con As New MySqlConnection("host=sql7.freemysqlhosting.net; username=sql7124107; password=censored; database=sql7124107")
Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader
con.Open()
cmd.Connection = con
cmd.CommandText = "select userid, password from login where userid='" & username & "' and password='" & password & "'"
dr = cmd.ExecuteReader
If dr.HasRows Then
'logged in
Me.Hide()
Main.Show()
Else
'not logged in
errortext.Visible = True
errortext.Text = "Invalid credentials, user dont exist."
End If
hash function:
VB.NET:
Public Function hash(str As String) As String
Try
Dim x As New System.Security.Cryptography.SHA512CryptoServiceProvider()
Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(str)
bs = x.ComputeHash(bs)
Dim s As New System.Text.StringBuilder()
For Each b As Byte In bs
s.Append(b.ToString("x2").ToLower())
Next
Return s.ToString()
Catch ex As Exception
errortext.Text = ex.ToString()
errortext.Visible = True
errortext.ForeColor = Color.Red
End Try
End Function
register function:
VB.NET:
For Each s As String In badkeywords
If usern.Text.Contains(s) Then
errortext.Text = "Your name contains a offensive word"
errortext.Visible = True
errortext.ForeColor = Color.Red
Return
End If
Next
If Not passw.Text = TextBox1.Text Then
errortext.Text = "Passwords doesn't match."
errortext.Visible = True
errortext.ForeColor = Color.Red
Return
End If
Dim username As String = hash(usern.Text)
Dim password As String = hash(passw.Text)
Try
Dim con As New MySqlConnection("host=sql7.freemysqlhosting.net; username=sql7124107; password=censored; database=sql7124107")
Dim cmd As New MySqlCommand
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into login(userid,password) values ('" & username & "','" & password & "')"
cmd.ExecuteNonQuery()
errortext.Text = "Successfully registered!"
errortext.ForeColor = Color.Green
errortext.Visible = True
Catch ex As Exception
errortext.Text = ex.ToString()
errortext.Visible = True
errortext.ForeColor = Color.Red
End Try
Edit:
I found that the output hash was to long. It works with sha-1 but not sha512. But i still think it should work with sha512 so if anyone could help me i would appreciate it.
Last edited: