Search code examples
c#hashsha256

c# SHA-256 Hash


I'm trying to use elastic transcoder of Amazon. Here I need to sha-256 hash a string; http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

I have tried any ways I have found on the net but I couldn't find the same result as page and some online hashing sites provides.

Here is the string to hash which you can find from the link above;

POST
/

content-type:application/x-www-form-urlencoded; charset=utf-8
host:iam.amazonaws.com
x-amz-date:20110909T233600Z

content-type;host;x-amz-date
b6359072c78d70ebee1e81adcbab4f01bf2c23245fa365ef83fe8f1f955085e2

Here is the expected result:

3511de7e95d28ecd39e9513b642aee07e54f4941150d8df8bf94b328ef7e55e2

I have tried many c# method but couldn't get this result; but these online sites provides same results;

http://crypo.in.ua/tools/eng_sha256.php

Here is one of my method;

public static string getHashSha256(string text)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(text);
        SHA256Managed hashstring = new SHA256Managed();
        byte[] hash = hashstring.ComputeHash(bytes);
        string hashString = string.Empty;
        foreach (byte x in hash)
        {
            hashString += String.Format("{0:x2}", x);
        }
        return hashString;
    }

Solution

  • Removing \r is the solution.

    s = s.Replace("\r", "");