Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address feedback on File types EE support #69102

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/Symbols/FileIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Security.Cryptography;
using System.Text;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CSharp;

internal struct FileIdentifier
internal readonly struct FileIdentifier
{
private static readonly Encoding s_encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

Expand All @@ -27,8 +27,8 @@ public static FileIdentifier Create(string filePath)
try
{
var encodedFilePath = s_encoding.GetBytes(filePath);
using var sha256 = SHA256.Create();
hash = sha256.ComputeHash(encodedFilePath).ToImmutableArray();
using var hashAlgorithm = SourceHashAlgorithms.CreateDefaultInstance();
hash = hashAlgorithm.ComputeHash(encodedFilePath).ToImmutableArray();
}
catch (EncoderFallbackException ex)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Compilers/Core/Portable/Text/SourceHashAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Security.Cryptography;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Text
Expand Down Expand Up @@ -44,5 +45,20 @@ public static SourceHashAlgorithm GetSourceHashAlgorithm(Guid guid)
=> (guid == s_guidSha256) ? SourceHashAlgorithm.Sha256 :
(guid == s_guidSha1) ? SourceHashAlgorithm.Sha1 :
SourceHashAlgorithm.None;

private static HashAlgorithm CreateInstance(SourceHashAlgorithm algorithm)
{
return algorithm switch
{
SourceHashAlgorithm.Sha1 => SHA1.Create(),
SourceHashAlgorithm.Sha256 => SHA256.Create(),
_ => throw ExceptionUtilities.UnexpectedValue(algorithm)
};
}

public static HashAlgorithm CreateDefaultInstance()
{
return CreateInstance(Default);
}
}
}