Skip to content

Commit

Permalink
[Runtime compilation] ChecksumValidator: Support checksum SHA256 even…
Browse files Browse the repository at this point in the history
… when the metadata uses Sha256 (dotnet#54446)

* ChecksumValidator: Support checksum SHA256 even when the metadata indicate Sha256
  • Loading branch information
yepeekai authored Jul 5, 2024
1 parent 2291eaf commit 06533a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/Mvc/Mvc.Razor.RuntimeCompilation/src/ChecksumValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static bool IsItemValid(RazorProjectFileSystem fileSystem, RazorCompiledI
}

var sourceDocumentChecksum = ComputeChecksum(projectItem, primaryChecksum.ChecksumAlgorithm);
if (!string.Equals(sourceDocumentChecksum.algorithm, primaryChecksum.ChecksumAlgorithm) ||
if (!string.Equals(sourceDocumentChecksum.algorithm, primaryChecksum.ChecksumAlgorithm, StringComparison.OrdinalIgnoreCase) ||
!ChecksumsEqual(primaryChecksum.Checksum, sourceDocumentChecksum.checksum))
{
// Main file exists, but checksums not equal.
Expand All @@ -80,7 +80,7 @@ public static bool IsItemValid(RazorProjectFileSystem fileSystem, RazorCompiledI
}

sourceDocumentChecksum = ComputeChecksum(importItem, checksum.ChecksumAlgorithm);
if (!string.Equals(sourceDocumentChecksum.algorithm, checksum.ChecksumAlgorithm) ||
if (!string.Equals(sourceDocumentChecksum.algorithm, checksum.ChecksumAlgorithm, StringComparison.OrdinalIgnoreCase) ||
!ChecksumsEqual(checksum.Checksum, sourceDocumentChecksum.checksum))
{
// Import file exists, but checksums not equal.
Expand All @@ -99,16 +99,15 @@ private static (byte[] checksum, string algorithm) ComputeChecksum(RazorProjectI
string algorithmName;

//only SHA1 and SHA256 are supported. Default to SHA1
switch (checksumAlgorithm)
if (nameof(SHA256).Equals(checksumAlgorithm, StringComparison.OrdinalIgnoreCase))
{
case nameof(SHA256):
hashData = SHA256.HashData;
algorithmName = nameof(SHA256);
break;
default:
hashData = SHA1.HashData;
algorithmName = nameof(SHA1);
break;
hashData = SHA256.HashData;
algorithmName = nameof(SHA256);
}
else
{
hashData = SHA1.HashData;
algorithmName = nameof(SHA1);
}

using (var stream = projectItem.Read())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void IsItemValid_AllFilesMatch_UsingSHA256_ReturnsTrue()
{
new RazorSourceChecksumAttribute("SHA256", GetChecksumSHA256("some other import"), "/Views/_ViewImports.cstml"),
new RazorSourceChecksumAttribute("SHA1", GetChecksum("some import"), "/Views/Home/_ViewImports.cstml"),
new RazorSourceChecksumAttribute("SHA256", GetChecksumSHA256("some content"), "/Views/Home/Index.cstml"),
new RazorSourceChecksumAttribute("Sha256", GetChecksumSHA256("some content"), "/Views/Home/Index.cstml"),
});

ProjectFileSystem.Add(new TestRazorProjectItem("/Views/Home/Index.cstml", "some content"));
Expand Down

0 comments on commit 06533a6

Please sign in to comment.