MetaGuru
MetaGuru

Reputation: 43813

Can someone convert this Hashing method from C# to VB?

  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;
}

THANKS!

VStudio keeps yelling at me for just what I have so far most specifically the bracket at the end of Byte?:

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

End Sub

Upvotes: 0

Views: 406

Answers (5)

JHBlues76
JHBlues76

Reputation: 21

Try changing the brackets to parentheses:

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

Upvotes: 0

Marc
Marc

Reputation: 9344

In addition to Andrew's answer, there are quite a few simple converter tools on the web. I tend to use this one with good success when needed.

Upvotes: 2

Quinn Wilson
Quinn Wilson

Reputation: 8263

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

Upvotes: 0

swolff1978
swolff1978

Reputation: 1865

Did you try

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

no semicolon?

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351456

How about this?

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 doesn't use [] for arrays, it uses () instead.

Upvotes: 8

Related Questions