提交 2e96a57e 编写于 作者: H Heejae Chang

guard us from crashing when VS ran under environment where SHA256 is not allowed

上级 950f43b4
......@@ -21,12 +21,16 @@ internal class DiagnosticAnalyzerLogger
private const string AnalyzerException = "Analyzer.Exception";
private const string AnalyzerExceptionHashCode = "Analyzer.ExceptionHashCode";
private static readonly SHA256CryptoServiceProvider s_sha256CryptoServiceProvider = new SHA256CryptoServiceProvider();
private static readonly SHA256CryptoServiceProvider s_sha256CryptoServiceProvider = GetSha256CryptoServiceProvider();
private static readonly ConditionalWeakTable<DiagnosticAnalyzer, StrongBox<bool>> s_telemetryCache = new ConditionalWeakTable<DiagnosticAnalyzer, StrongBox<bool>>();
private static string ComputeSha256Hash(string name)
{
if (s_sha256CryptoServiceProvider == null)
{
return "Hash Provider Not Available";
}
byte[] hash = s_sha256CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(name));
return Convert.ToBase64String(hash);
}
......@@ -170,5 +174,18 @@ private static bool CheckTelemetry(DiagnosticAnalyzerService service, Diagnostic
DiagnosticDescriptor diagnostic = diagDescriptors.Length > 0 ? diagDescriptors[0] : null;
return diagnostic == null ? false : diagnostic.CustomTags.Any(t => t == WellKnownDiagnosticTags.Telemetry);
}
private static SHA256CryptoServiceProvider GetSha256CryptoServiceProvider()
{
try
{
// not all environment allows SHA256 encryption
return new SHA256CryptoServiceProvider();
}
catch
{
return null;
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册