0

以上是 VB.NET 的 SHA1 哈希函数。

Function getSHA1Hash(ByVal strToHash As String) As String
        Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
        bytesToHash = sha1Obj.ComputeHash(bytesToHash)
        Dim strResult As String = ""
        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next
        Return strResult
End Function

请有人解释上面的代码(Visual Basic .NET),特别是下面的行 -

bytesToHash = sha1Obj.ComputeHash(bytesToHash)
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
4

2 回答 2

0

SHA1 创建一个表示值 strToHash 的散列(字节数组)。foreach 只是将这个字节数组转换为字符串。

于 2013-10-15T09:45:54.017 回答
0

创建一个包含哈希的字节数组

bytesToHash = sha1Obj.ComputeHash(bytesToHash)

循环遍历刚刚创建的每个字节

For Each b As Byte In bytesToHash

将每个字节的十六进制值附加到字符串

strResult += b.ToString("x2")

请参阅 ToString 的十六进制格式:http: //msdn.microsoft.com/en-us/library/dwhawy9k.aspx#XFormatString

于 2013-10-15T09:56:41.593 回答