From 5b2f0fbf59e603b44621f84c1ca123c302e016c4 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 25 Jul 2017 11:26:26 +0800 Subject: [PATCH] CONTENT-8414: compatible with .net 35. --- Components/Checks/CheckDefaultPage.cs | 7 ----- Components/Utility.cs | 38 +++++++++++++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Components/Checks/CheckDefaultPage.cs b/Components/Checks/CheckDefaultPage.cs index 6882294..bf458ba 100644 --- a/Components/Checks/CheckDefaultPage.cs +++ b/Components/Checks/CheckDefaultPage.cs @@ -14,13 +14,6 @@ public class CheckDefaultPage : IAuditCheck public CheckResult Execute() { var result = new CheckResult(SeverityEnum.Unverified, "CheckDefaultPage"); - var version = Environment.Version.ToString(2); - if (version == "2.0") - { - result.Notes.Add("This check requires .Net Version 4.0 or above"); - return result; - } - try { IList modifiedFiles; diff --git a/Components/Utility.cs b/Components/Utility.cs index 2af2d6c..1213b6c 100644 --- a/Components/Utility.cs +++ b/Components/Utility.cs @@ -182,7 +182,7 @@ public static XmlDocument LoadFileSumData() public static string GetFileCheckSum(string fileName) { - using (var cryptographyProvider = SHA256.Create(AllowOnlyFipsAlgorithms() ? "System.Security.Cryptography.SHA256CryptoServiceProvider" : "System.Security.Cryptography.SHA256Cng")) + using (var cryptographyProvider = CreateCryptographyProvider()) { if (cryptographyProvider != null) { @@ -197,6 +197,29 @@ public static string GetFileCheckSum(string fileName) return string.Empty; } + private static SHA256 CreateCryptographyProvider() + { + try + { + var property = typeof(CryptoConfig).GetProperty("AllowOnlyFipsAlgorithms", BindingFlags.Public | BindingFlags.Static); + if (property == null) + { + return SHA256.Create(); + } + + if ((bool) property.GetValue(null, null)) + { + return SHA256.Create("System.Security.Cryptography.SHA256CryptoServiceProvider"); + } + + return SHA256.Create("System.Security.Cryptography.SHA256Cng"); + } + catch (Exception) + { + return null; + } + } + public static string GetApplicationVersion() { return DotNetNukeContext.Current.Application.Version.ToString(3); @@ -253,18 +276,5 @@ public static IList GetLastModifiedExecutableFiles() .Take(ModifiedFilesCount).ToList(); } - - private static bool AllowOnlyFipsAlgorithms() - { - try - { - var property = typeof(CryptoConfig).GetProperty("AllowOnlyFipsAlgorithms", BindingFlags.Public | BindingFlags.Static); - return property != null && (bool)property.GetValue(null, null); - } - catch (Exception) - { - return false; - } - } } } \ No newline at end of file