【问题标题】:C# SRI Hash GeneratorC# SRI 哈希生成器
【发布时间】:2020-12-21 06:40:32
【问题描述】:

我想创建一个 C# 函数,它生成一个有效的 url 的 SRI 哈希。 在这里查看更多信息:https://www.srihash.org/

我发现的所有示例都使用 OpenSSL, 像“openssl dgst -sha384 -binary FILENAME.js | openssl base64 -A” 但我想用 C# 来做。

我已经尝试使用以下代码,但当我将结果与 srihash.org 进行比较时,它似乎不正确:

string url = "https://code.jquery.com/jquery-3.5.1.js";
string source;

using (WebClient client = new WebClient())
{
    source = client.DownloadString(url);
}

using (SHA384 sha384Hash = SHA384.Create())
{
    //From String to byte array
    byte[] sourceBytes = Encoding.UTF8.GetBytes(source);
    byte[] hashBytes = sha384Hash.ComputeHash(sourceBytes);
    string hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty);

    Console.WriteLine("The SHA384 hash of " + source + " is: " + hash);
    Console.ReadLine();
}

【问题讨论】:

    标签: c# hash


    【解决方案1】:

    我找到了解决办法。

    这行得通:

    string url = "https://code.jquery.com/jquery-3.5.1.js";
    
    var fs = new MemoryStream(new WebClient().DownloadData(url));
    using (SHA384 sha384Hash = SHA384.Create())
    {
        Console.WriteLine("The SHA384 hash is " + Convert.ToBase64String(sha384Hash.ComputeHash(fs)));
        Console.ReadLine();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-16
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 2021-01-06
      • 2016-08-13
      • 1970-01-01
      相关资源
      最近更新 更多