有人可以将此Hashing方法从C#转换为VB吗?

时间:2009-08-21 18:17:44

标签: c# vb.net

  public static string CalculateSHA1(string text, Encoding enc)
{
    byte[] buffer = enc.GetBytes(text);
    SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
    string hash = BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
    return hash;
}

谢谢!

VStudio一直对我大吼大叫,因为我到目前为止最特别是Byte末尾的括号?:

Private Sub CalculateSHA1(ByVal text As String, ByVal enc As Encoding)
    Dim buffer As Byte[] = enc.GetBytes(text);

End Sub

5 个答案:

答案 0 :(得分:8)

这个怎么样?

Public Shared Function CalculateSHA1(text As String, enc As Encoding) As String
    Dim buffer As Byte() = enc.GetBytes(text)
    Dim cryptoTransformSHA1 As New SHA1CryptoServiceProvider()
    Dim hash As String = BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "")
    Return hash
End Function

VB.NET不对数组使用[],而是使用()代替。

答案 1 :(得分:2)

答案 2 :(得分:0)

你试过吗

Dim buffer as Byte() = enc.GetBytes(text)

没有分号?

答案 3 :(得分:0)

Public Shared Function CalculateSHA1(ByVal text As String, ByVal enc As Encoding) As String
    Dim buffer As Byte() = enc.GetBytes(text)
    Dim cryptoTransformSHA1 As New SHA1CryptoServiceProvider()
    Dim hash As String = BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "")
    Return hash
End Function

答案 4 :(得分:0)

尝试将括号更改为括号:

Dim buffer As Byte() = enc.GetBytes(text);