jhunter
jhunter

Reputation: 1892

SRI hash not what is expected

I am trying to implement subresource integrity in our build process, so I'm generating the hash keys to our javascript files myself. On most of our files the hash I'm generating matches what the browsers expect and what srihash.org outputs. However, for jquery and jquery-ui I'm generating a different hash than the browser is expecting and srihash.org is outputting. Here is my hashing code:

private static string GetSriHash(string text) {
    var enc = Encoding.UTF8;
    var result = new SHA384Managed().ComputeHash(enc.GetBytes(text));
    return Convert.ToBase64String(result);
}

Where 'text' is the contents of the file. I thought it was some kind of encoding issue, but I tried every option in Encoding and none of them matched. I've tried reading the file off the file system and getting it from a server using WebClient.DownloadString and I get the same (incorrect) hash every time.

Upvotes: 4

Views: 971

Answers (1)

jhunter
jhunter

Reputation: 1892

I found the solution... If I change the code to get the file with File.ReadAllBytes() or WebClient.DownloadData() it works fine.

Upvotes: 4

Related Questions