I'm having a great deal of difficulty finding a function or sub for vb (not C) that provides an easy way to convert a given string into a sha-1 (or sha512 ideally) hash.
If someone could provide a function in VB, it'd be extremely helpful.
Nearest attempt:
Function create_hash(ByVal password, ByVal salt)
Dim input As [Char]() = "string to hash".ToCharArray()
Dim secret As New SecureString()
For idx As Integer =开发者_JAVA百科 0 To input.Length - 1
secret.AppendChar(input(idx))
Next SecurePassword.MakeReadOnly()
Dim pBStr As IntPtr = Marshal.SecureStringToBSTR(secret)
Dim output As String = Marshal.PtrToStringBSTR(pBStr)
Marshal.FreeBSTR(pBStr)
Dim sha As SHA512 = New SHA512Managed()
Dim result As Byte() = sha.ComputeHash(Encoding.UTF8.GetBytes(output))
Return result
End Function
But this causes visual stuido to blue underline SecurePassword and Marshal at evey use. These are marked as undeclared variables, but declaring them causes other problems which I can't find a way to solve.
Take a look at the documentation for the System.Security.Cryptography.SHA512 class.
Dim data(DATA_SIZE) As Byte
Dim result() As Byte
Dim shaM As New SHA512Managed()
result = shaM.ComputeHash(data)
Like this (ignoring the salting you are doing at the top):
Function create_hash(ByVal password, ByVal salt) As Byte()
Dim sha As SHA512 = New SHA512Managed()
Dim result As Byte() = sha.ComputeHash(Encoding.UTF8.GetBytes(password & salt))
Return result
End Function
精彩评论