user1192078
user1192078

Reputation: 41

Code Conversion

I want to convert this vb into c#, I cant correct it

Public Function encryptPassword(ByVal Password As String, ByVal Salt As String) As String
  Using HashTool As New SHA512Managed()
      Dim PasswordAsByte() As Byte = System.Text.Encoding.UTF8.GetBytes(String.Concat(Password, Salt))
      Dim EncryptedBytes() As Byte = HashTool.ComputeHash(PasswordAsByte)
      HashTool.Clear()
      Return Convert.ToBase64String(EncryptedBytes)
  End Using
End Function

*I was suggested this code, *

public string encryptPassword(string Password, string Salt) {
    Using;
    ((void)(HashTool));
    new SHA512Managed();
    byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(string.Concat(Password, Salt));
    byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);
    HashTool.Clear();
    return Convert.ToBase64String(EncryptedBytes);
}

but it wasn't correct at all, please help

Upvotes: 1

Views: 141

Answers (1)

cHao
cHao

Reputation: 86525

Not sure who suggested that code; it looks like a half-assed decompiler or conversion tool. But it's pretty close, once you weed out the weirdness...apparently someone just doesn't know how to decompile a using block. :P

public string encryptPassword(string Password, string Salt)
{
    using (var HashTool = new SHA512Managed())
    {
        byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(string.Concat(Password, Salt));
        byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);
        HashTool.Clear();
        return Convert.ToBase64String(EncryptedBytes);
    }
}

(Side note: You're hashing, not encrypting. EncryptedBytes is badly named. Oh, and naming conventions say the variable names should start with a lowercase letter.)

Upvotes: 4

Related Questions