Unit test SHA256 wrapper queries

Posted by Sam Leach on Programmers See other posts from Programmers or by Sam Leach
Published on 2013-07-02T10:35:11Z Indexed on 2013/07/02 11:13 UTC
Read the original article Hit count: 344

Filed under:
|
|
|

I am just beginning to write unit tests. So please bear with me. I have the following SHA256 wrapper.

    public static string SHA256(string plainText)
    {
        StringBuilder sb = new StringBuilder();

        SHA256CryptoServiceProvider provider = new SHA256CryptoServiceProvider();
        var hashedBytes = provider.ComputeHash(Encoding.UTF8.GetBytes(plainText));

        for (int i = 0; i < hashedBytes.Length; i++)
        {
            sb.Append(hashedBytes[i].ToString("x2").ToLower());
        }

        return sb.ToString();
    }

Do I want to be testing it? If so, what do you recommend?

My thought process is as follows:

  1. What logic is there here.
  2. The answer is my for loop and ToString("x2") so from my understanding I want to be testing this part?

I can assume Encoding.UTF8.GetBytes(plainText) works. Correct assumption? I can assume SHA256CryptoServiceProvider.ComputeHash() works. Correct assumption?

I want to be only testing my logic. In this case is limited to the printing of hex encoded hash. Correct?

Thanks.

© Programmers or respective owner

Related posts about c#

Related posts about testing