Jump to content

Outputting the correct salt

AliB2512

I have code that'll take a password, generate a random salt, combine the 2 then hash with sha512. it then saves the username, hashed password, and salt to a txt file.

however, I cannot get it to print the salt used with the password and not another randomly generated salt.

 

Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
  Public Class Users
   Public Function CreateRandomSalt() As String
    Dim mix As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+=][}{<>"
    Dim salt As String = ""
    Dim rnd As New Random
    Dim sb As New StringBuilder
    For i As Integer = 1 To 100
        Dim x As Integer = rnd.Next(0, mix.Length - 1)
        salt &= (mix.Substring(x, 1))
    Next
    Return salt
End Function

Public Function Hash512(password As String, salt As String) As String
    globalVariable.Salty = Convert.ToBase64String(Encoding.UTF8.GetBytes(salt))
    Dim convertedToBytes As Byte() = Encoding.UTF8.GetBytes(password & salt)
    Dim hashType As HashAlgorithm = New SHA512Managed()
    Dim hashBytes As Byte() = hashType.ComputeHash(convertedToBytes)
    Dim hashedResult As String = Convert.ToBase64String(hashBytes)
    Return hashedResult
End Function
Private Sub save_Click(sender As Object, e As EventArgs) Handles save.Click
    Dim path As String
    path = "..\Resources\Users\" + txtUsername.Text + ".txt"
    MsgBox(path)
    If System.IO.File.Exists(path) Then

        MsgBox("File Already Exists)")
    Else
        File.Create(path).Dispose()
        Dim objWriter As New System.IO.StreamWriter(path)
        objWriter.WriteLine(txtsave.Text)
        objWriter.WriteLine((Hash512(txtUsername.Text, CreateRandomSalt)))
        objWriter.WriteLine(globalVariable.Salty)
        objWriter.Close()
        MsgBox("User added")


    End If
 End Sub


End Class
Public Class globalVariable
  Public Shared Salty As String
End Class

 

Link to comment
Share on other sites

Link to post
Share on other sites

store the hash and salt seperated by a ',' or ':' then explode into an array

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, vorticalbox said:

store the hash and salt seperated by a ',' or ':' then explode into an array

care to elaborate?

 

example

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, AliB2512 said:

care to elaborate?

 

example

so you're saving username, password and salt to a txt file that is on a new line. So check if the file is there if it is then pull the data into an array and print out if the file doesn't exist then go about your normal password hashing stuff.

 

basically don't create anything until you have checked for data first.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×