c# SHA-256 哈希

c# SHA-256 Hash

我正在尝试使用亚马逊的弹性转码器。 这里我需要对一个字符串进行sha-256哈希; http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

我已经尝试了我在网上找到的所有方法,但找不到与页面和一些在线哈希网站提供的相同结果。

这是您可以从上面的 link 中找到的要散列的字符串;

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

这是预期的结果:

3511de7e95d28ecd39e9513b642aee07e54f4941150d8df8bf94b328ef7e55e2

我试过很多c#方法都得不到这个结果; 但这些在线网站提供了相同的结果;

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

这是我的方法之一;

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

删除 \r 是解决方案 s = s.Replace("\r", "");