SHA类型设置为SHA512哈希

时间:2011-06-21 09:55:27

标签: vb.net types type-conversion

我有以下用于生成sha512哈希的函数。哈希是成功生成的,但是当结果字符串传递给其他函数时会导致此错误:

  

输入字符串的格式不正确

调试时,保存返回的哈希值(设置为字符串)的变量为空。我已经尝试在函数和调用代码中将类型更改为int,int64和byte(数组和标准变量),这会导致各种其他错误。如何正确更改数据类型以解决此问题?

Function create_hash(ByVal password, ByVal salt)

Dim salty As String = password & salt

'convert salty password to binary to feed into hash function
Dim encText As New System.Text.UTF8Encoding()
Dim btText() As Byte
btText = encText.GetBytes(salty)


'Dim data(btText) As Byte
'create password hash
Dim result() As Byte
Dim shaM As New SHA512Managed()
result = shaM.ComputeHash(btText)

Dim return_result As String

For Each Item As Integer In result

    return_result = return_result & Item

Next



Return return_result
End Function

致电代码:

Dim i_h_pass As String
Dim i_pass As String = pass.Text
            'handle password generation (matching passwords checked at validation)
            Dim newHash = New hashing
            Dim salt As String = Convert.ToString(newHash.create_salt)
            i_h_pass = Convert.ToString(newHash.create_hash(i_pass, salt))

修改

还检查了create_salt函数 - 它工作正常并返回一个随机整数,作为字符串返回以进行召集

1 个答案:

答案 0 :(得分:2)

修正:

功能create_hash(ByVal密码,ByVal盐)

Dim salty As String = password & salt

'convert salty password to binary to feed into hash function
Dim encText As New System.Text.UTF8Encoding()
Dim btText() As Byte
btText = encText.GetBytes(salty)


'Dim data(btText) As Byte
'create password hash
Dim result() As Byte
Dim shaM As New SHA512Managed()
result = shaM.ComputeHash(btText)

Dim return_result As String = BitConverter.ToString(result)
    Return return_result
End Function

Dim return_result As String = BitConverter.ToString(result)

做出改变