2016-01-06 120 views
0

在我的web應用程序,我創建一個隨機鹽,後來我通過這種鹽的哈希函數,就像這樣:等效密碼哈希函數vb.net

function generate_salt() 
{ 
    $max_length = 100; 
    $salt = hash('sha256', (uniqid(rand(), true))); 
    return substr($salt, 0, $max_length); 
} 

function hash_password($salt, $password) 
{ 
    $half = (int)(strlen($salt)/2); 
    $hash = hash('sha256', substr($salt, 0, $half) . $password . substr($salt, $half)); 

    for ($i = 0; $i < 100000; $i++) 
    { 
     $hash = hash('sha256', $hash); 
    } 

    return $hash; 
} 

這是我vb.net功能:

Public Shared Function CreateRandomSalt() As String 

    Dim mix As String = "[email protected]#$%^&*()_+=][}{<>" 
    Dim salt As String = "" 
    Dim rnd As New Random 
    Dim sb As New StringBuilder 

    For i As Integer = 1 To 100 'Lunghezza del salt 
     Dim x As Integer = rnd.Next(0, mix.Length - 1) 
     salt &= (mix.Substring(x, 1)) 
    Next 

    Return salt 

End Function 

Public Shared Function Hash512(password As String, salt As String) As String 

    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 

現在我的問題是,當我創建一個新用戶或從我vb.net的應用程序更新爲特定用戶的密碼,Web應用程序不能執行登錄我的用戶,也許密碼編碼是不同的,所以我需要一個012的功能或php,讓我有相同的編碼。我該怎麼做?

+0

[A PHP等效爲一個VB.NET密碼哈希函數(的可能的複製http://stackoverflow.com/questions/ 19457726/a-php-equivalent-for-a-vb-net-password-hash-function) – Plutonix

+1

但是我有一個編碼爲256的 –

+0

你是否儲存鹽或每次用戶嘗試登錄時生成一個隨機數? –

回答

0

創建的Md5密碼:

Public Function MD5(ByVal pass As String) As String 
     Try 
      Dim MD5p As New System.Security.Cryptography.MD5CryptoServiceProvider 
      Dim baytlar As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(pass) 
      Dim hash As Byte() = MD5p.ComputeHash(baytlar) 
      Dim kapasite As Integer = (hash.Length * 2 + (hash.Length/8)) 
      Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder(kapasite) 
      Dim I As Integer 
      For I = 0 To hash.Length - 1 
       sb.Append(BitConverter.ToString(hash, I, 1)) 
      Next I 
      Return sb.ToString().TrimEnd(New Char() {" "c}) 
     Catch ex As Exception 
      Return "0" 
     End Try 
    End Function 

插入或控制:MD5(Password String)

+0

這可以幫助我嗎? –