VB.Net用Salt哈希数据的实例
效果图:
代码如下:
Imports System
Imports System.Text
Imports System.Security.Cryptography
Module Module1
Public Class SimpleHash
Public Shared Function ComputeHash(ByVal plainText As String, _
ByVal hashAlgorithm As String, _
ByVal saltBytes() As Byte) _
As String
If (saltBytes Is Nothing) Then
Dim minSaltSize As Integer
Dim maxSaltSize As Integer
minSaltSize = 4
maxSaltSize = 8
Dim random As Random
random = New Random()
Dim saltSize As Integer
saltSize = random.Next(minSaltSize, maxSaltSize)
saltBytes = New Byte(saltSize - 1){}
Dim rng As RNGCryptoServiceProvider
rng = New RNGCryptoServiceProvider()
rng.GetNonZeroBytes(saltBytes)
End If
Dim plainTextBytes As Byte()
plainTextBytes = Encoding.UTF8.GetBytes(plainText)
Dim plainTextWithSaltBytes() As Byte = _
New Byte(plainTextBytes.Length + saltBytes.Length - 1){}
Dim I As Integer
For I = 0 To plainTextBytes.Length - 1
plainTextWithSaltBytes(I) = plainTextBytes(I)
Next I
For I = 0 To saltBytes.Length - 1
plainTextWithSaltBytes(plainTextBytes.Length + I) = saltBytes(I)
Next I
Dim hash As HashAlgorithm
If (hashAlgorithm Is Nothing) Then
hashAlgorithm = ""
End If
Select hashAlgorithm.ToUpper()
Case "SHA1"
hash = New SHA1Managed()
Case "SHA256"
hash = New SHA256Managed()
Case "SHA384"
hash = New SHA384Managed()
Case "SHA512"
hash = New SHA512Managed()
Case Else
hash = New MD5CryptoServiceProvider()
End Select
Dim hashBytes As Byte()
hashBytes = hash.ComputeHash(plainTextWithSaltBytes)
Dim hashWithSaltBytes() As Byte = _
New Byte(hashBytes.Length + _
saltBytes.Length - 1) {}
For I = 0 To hashBytes.Length - 1
hashWithSaltBytes(I) = hashBytes(I)
Next I
For I = 0 To saltBytes.Length - 1
hashWithSaltBytes(hashBytes.Length + I) = saltBytes(I)
Next I
Dim hashValue As String
hashValue = Convert.ToBase64String(hashWithSaltBytes)
ComputeHash = hashValue
End Function
Public Shared Function VerifyHash(ByVal plainText As String, _
ByVal hashAlgorithm As String, _
ByVal hashValue As String) _
As Boolean
Dim hashWithSaltBytes As Byte()
hashWithSaltBytes = Convert.FromBase64String(hashValue)
Dim hashSizeInBits As Integer
Dim hashSizeInBytes As Integer
If (hashAlgorithm Is Nothing) Then
hashAlgorithm = ""
End If
Select hashAlgorithm.ToUpper()
Case "SHA1"
hashSizeInBits = 160
Case "SHA256"
hashSizeInBits = 256
Case "SHA384"
hashSizeInBits = 384
Case "SHA512"
hashSizeInBits = 512
Case Else
hashSizeInBits = 128
End Select
hashSizeInBytes = hashSizeInBits / 8
If (hashWithSaltBytes.Length < hashSizeInBytes) Then
VerifyHash = False
End If
Dim saltBytes() As Byte = New Byte(hashWithSaltBytes.Length - _
hashSizeInBytes - 1) {}
Dim I As Integer
For I = 0 To saltBytes.Length - 1
saltBytes(I) = hashWithSaltBytes(hashSizeInBytes + I)
Next I
Dim expectedHashString As String
expectedHashString = ComputeHash(plainText, hashAlgorithm, saltBytes)
VerifyHash = (hashValue = expectedHashString)
End Function
End Class
Sub Main()
Dim password As String
Dim wrongPassword As String
password = "myP@5sw0rd"
wrongPassword = "password"
Dim passwordHashMD5 As String
Dim passwordHashSha1 As String
Dim passwordHashSha256 As String
Dim passwordHashSha384 As String
Dim passwordHashSha512 As String
passwordHashMD5 = _
SimpleHash.ComputeHash(password, "MD5", Nothing)
passwordHashSha1 = _
SimpleHash.ComputeHash(password, "SHA1", Nothing)
passwordHashSha256 = _
SimpleHash.ComputeHash(password, "SHA256", Nothing)
passwordHashSha384 = _
SimpleHash.ComputeHash(password, "SHA384", Nothing)
passwordHashSha512 = _
SimpleHash.ComputeHash(password, "SHA512", Nothing)
Console.WriteLine("COMPUTING HASH VALUES")
Console.WriteLine("")
Console.WriteLine("MD5 : {0}", passwordHashMD5)
Console.WriteLine("SHA1 : {0}", passwordHashSha1)
Console.WriteLine("SHA256: {0}", passwordHashSha256)
Console.WriteLine("SHA384: {0}", passwordHashSha384)
Console.WriteLine("SHA512: {0}", passwordHashSha512)
Console.WriteLine("")
Console.WriteLine("COMPARING PASSWORD HASHES")
Console.WriteLine("")
Console.WriteLine("MD5 (good): {0}", _
SimpleHash.VerifyHash( _
password, "MD5", _
passwordHashMD5).ToString())
Console.WriteLine("MD5 (bad) : {0}", _
SimpleHash.VerifyHash( _
wrongPassword, "MD5", _
passwordHashMD5).ToString())
Console.WriteLine("SHA1 (good): {0}", _
SimpleHash.VerifyHash( _
password, "SHA1", _
passwordHashSha1).ToString())
Console.WriteLine("SHA1 (bad) : {0}", _
SimpleHash.VerifyHash( _
wrongPassword, "SHA1", _
passwordHashSha1).ToString())
Console.WriteLine("SHA256 (good): {0}", _
SimpleHash.VerifyHash( _
password, "SHA256", _
passwordHashSha256).ToString())
Console.WriteLine("SHA256 (bad) : {0}", _
SimpleHash.VerifyHash( _
wrongPassword, "SHA256", _
passwordHashSha256).ToString())
Console.WriteLine("SHA384 (good): {0}", _
SimpleHash.VerifyHash( _
password, "SHA384", _
passwordHashSha384).ToString())
Console.WriteLine("SHA384 (bad) : {0}", _
SimpleHash.VerifyHash( _
wrongPassword, "SHA384", _
passwordHashSha384).ToString())
Console.WriteLine("SHA512 (good): {0}", _
SimpleHash.VerifyHash( _
password, "SHA512", _
passwordHashSha512).ToString())
Console.WriteLine("SHA512 (bad) : {0}", _
SimpleHash.VerifyHash( _
wrongPassword, "SHA512", _
passwordHashSha512).ToString())
End Sub
End Module
|