8

I have a class that encrypts a password with a salted hash.

But If I want to pass a null to the class I get the following error: Cannot implicitly convert type string to byte[]

Here is the class code:

public class MyHash
{
    public static string ComputeHash(string plainText, 
                            string hashAlgorithm, byte[] saltBytes)
    {
        Hash Code
    }
}

When I use the class I get the error: "Cannot implicitly convert type string to byte[]"

//Encrypt Password
byte[] NoHash = null;
byte[] encds = MyHash.ComputeHash(Password, "SHA256", NoHash);
1
  • Are strings supposed to be convertible to byte[]? Commented May 29, 2012 at 2:08

2 Answers 2

15

This is because your 'ComputeHash' method returns a string, and you are trying to assign this return value to a byte array with;

byte[] encds = MyHash.ComputeHash(Password, "SHA256", NoHash);

There is no implicit converstion for string to byte[] because there exist a number of different encodings to represent a string as bytes, such as ASCII or UTF8.

You need to explicitly convert the bytes using an appropriate encoding class like so;

string x = "somestring";
byte[] y = System.Text.Encoding.UTF8.GetBytes(x);
Sign up to request clarification or add additional context in comments.

Comments

0

The return type of your ComputeHash function is a string. You try to assign the result of your function to encds, which is byte[]. The compiler points this discrepancy out to you, because there is no implicit conversion from string to byte[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.