Skip to content

Commit

Permalink
Use Convert.ToHexStringLower (#38965)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi authored Feb 27, 2024
1 parent 59a518b commit 2c263c3
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/Sha256Hasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public static string Hash(string text)
{
byte[] bytes = Encoding.UTF8.GetBytes(text);
byte[] hash = SHA256.HashData(bytes);
#if NET9_0_OR_GREATER
return Convert.ToHexStringLower(hash);
#else
return Convert.ToHexString(hash).ToLowerInvariant();
#endif
}

public static string HashWithNormalizedCasing(string text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ internal static string GetSha(string str)
Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
SHA256.HashData(Encoding.UTF8.GetBytes(str), hash);

return Convert.ToHexString(hash).ToLowerInvariant();
return Convert.ToHexStringLower(hash);
}
}
4 changes: 2 additions & 2 deletions src/Containers/Microsoft.NET.Build.Containers/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ static UnixFileMode DetermineFileMode(FileSystemInfo file)
}
}

string contentHash = Convert.ToHexString(hash).ToLowerInvariant();
string uncompressedContentHash = Convert.ToHexString(uncompressedHash).ToLowerInvariant();
string contentHash = Convert.ToHexStringLower(hash);
string uncompressedContentHash = Convert.ToHexStringLower(uncompressedHash);

string layerMediaType = manifestMediaType switch
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/Microsoft.NET.Build.Tasks/AllowEmptyTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static string HashWithNormalizedCasing(string text)
#endif
#if NET
byte[] hash = System.Security.Cryptography.SHA256.HashData(utf8UpperBytes);
return Convert.ToHexString(hash).ToLowerInvariant();
return Convert.ToHexStringLower(hash);
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private static void VerifyDescriptorInfo(Layer l)
}
}

Assert.Equal(Convert.ToHexString(hashBytes), l.Descriptor.Digest.Substring("sha256:".Length), ignoreCase: true);
Assert.Equal(Convert.ToHexString(uncompressedHashBytes), l.Descriptor.UncompressedDigest?.Substring("sha256:".Length), ignoreCase: true);
Assert.Equal(Convert.ToHexStringLower(hashBytes), l.Descriptor.Digest.Substring("sha256:".Length));
Assert.Equal(Convert.ToHexStringLower(uncompressedHashBytes), l.Descriptor.UncompressedDigest?.Substring("sha256:".Length));
}

TransientTestFolder? testSpecificArtifactRoot;
Expand Down

0 comments on commit 2c263c3

Please sign in to comment.