【发布时间】: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();
}
【问题讨论】: