diff --git a/src/Compilers/CSharp/Portable/Symbols/FileIdentifier.cs b/src/Compilers/CSharp/Portable/Symbols/FileIdentifier.cs index 85e3302c000ae..2ad6757a501d0 100644 --- a/src/Compilers/CSharp/Portable/Symbols/FileIdentifier.cs +++ b/src/Compilers/CSharp/Portable/Symbols/FileIdentifier.cs @@ -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); @@ -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) { diff --git a/src/Compilers/Core/Portable/Text/SourceHashAlgorithms.cs b/src/Compilers/Core/Portable/Text/SourceHashAlgorithms.cs index 7ca4fd3067163..eb84be4dc4aa8 100644 --- a/src/Compilers/Core/Portable/Text/SourceHashAlgorithms.cs +++ b/src/Compilers/Core/Portable/Text/SourceHashAlgorithms.cs @@ -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 @@ -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); + } } }