2013-10-15 62 views
0

以上是VB.NET的SHA1哈希函數。Visual Basic中SHA-1哈希函數的語法解釋

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") 

回答

0
SHA1

創建表示值strToHash散列(字節陣列)。 foreach只是將這個字節數組轉換成一個字符串。

0

創建一個字節數組通過剛剛創建

For Each b As Byte In bytesToHash 

追加到串中的每個字節的十六進制值的每個字節包含散列

bytesToHash = sha1Obj.ComputeHash(bytesToHash) 

環路

strResult += b.ToString("x2") 

參見本換成十六進制格式的ToString:http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#XFormatString