summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/asn1/DerGeneralizedTime.cs9
-rw-r--r--crypto/src/openssl/PEMUtilities.cs12
-rw-r--r--crypto/src/security/CipherUtilities.cs12
-rw-r--r--crypto/src/security/DigestUtilities.cs5
-rw-r--r--crypto/src/security/WrapperUtilities.cs5
-rw-r--r--crypto/src/util/Enums.cs33
6 files changed, 36 insertions, 40 deletions
diff --git a/crypto/src/asn1/DerGeneralizedTime.cs b/crypto/src/asn1/DerGeneralizedTime.cs
index 22dc04912..898a3d585 100644
--- a/crypto/src/asn1/DerGeneralizedTime.cs
+++ b/crypto/src/asn1/DerGeneralizedTime.cs
@@ -282,15 +282,8 @@ namespace Org.BouncyCastle.Asn1
             DateTimeStyles style = DateTimeStyles.None;
             if (Platform.EndsWith(format, "Z"))
             {
-                try
-                {
-                    style = (DateTimeStyles)Enums.GetEnumValue(typeof(DateTimeStyles), "AssumeUniversal");
-                }
-                catch (Exception)
-                {
-                }
-
                 style |= DateTimeStyles.AdjustToUniversal;
+                style |= DateTimeStyles.AssumeUniversal;
             }
 
             DateTime dt = DateTime.ParseExact(s, format, DateTimeFormatInfo.InvariantInfo, style);
diff --git a/crypto/src/openssl/PEMUtilities.cs b/crypto/src/openssl/PEMUtilities.cs
index b58e5e765..332768083 100644
--- a/crypto/src/openssl/PEMUtilities.cs
+++ b/crypto/src/openssl/PEMUtilities.cs
@@ -16,8 +16,8 @@ namespace Org.BouncyCastle.OpenSsl
 		static PemUtilities()
 		{
 			// Signal to obfuscation tools not to change enum constants
-			((PemBaseAlg)Enums.GetArbitraryValue(typeof(PemBaseAlg))).ToString();
-			((PemMode)Enums.GetArbitraryValue(typeof(PemMode))).ToString();
+			Enums.GetArbitraryValue<PemBaseAlg>().ToString();
+            Enums.GetArbitraryValue<PemMode>().ToString();
 		}
 
 		private static void ParseDekAlgName(
@@ -31,16 +31,16 @@ namespace Org.BouncyCastle.OpenSsl
 
 				if (dekAlgName == "DES-EDE" || dekAlgName == "DES-EDE3")
 				{
-					baseAlg = (PemBaseAlg)Enums.GetEnumValue(typeof(PemBaseAlg), dekAlgName);
+					baseAlg = Enums.GetEnumValue<PemBaseAlg>(dekAlgName);
 					return;
 				}
 
 				int pos = dekAlgName.LastIndexOf('-');
 				if (pos >= 0)
 				{
-					baseAlg = (PemBaseAlg)Enums.GetEnumValue(typeof(PemBaseAlg), dekAlgName.Substring(0, pos));
-					mode = (PemMode)Enums.GetEnumValue(typeof(PemMode), dekAlgName.Substring(pos + 1));
-					return;
+					baseAlg = Enums.GetEnumValue<PemBaseAlg>(dekAlgName.Substring(0, pos));
+                    mode = Enums.GetEnumValue<PemMode>(dekAlgName.Substring(pos + 1));
+                    return;
 				}
 			}
 			catch (ArgumentException)
diff --git a/crypto/src/security/CipherUtilities.cs b/crypto/src/security/CipherUtilities.cs
index 929040e2c..8fbf19218 100644
--- a/crypto/src/security/CipherUtilities.cs
+++ b/crypto/src/security/CipherUtilities.cs
@@ -118,9 +118,9 @@ namespace Org.BouncyCastle.Security
         static CipherUtilities()
         {
             // Signal to obfuscation tools not to change enum constants
-            ((CipherAlgorithm)Enums.GetArbitraryValue(typeof(CipherAlgorithm))).ToString();
-            ((CipherMode)Enums.GetArbitraryValue(typeof(CipherMode))).ToString();
-            ((CipherPadding)Enums.GetArbitraryValue(typeof(CipherPadding))).ToString();
+            Enums.GetArbitraryValue<CipherAlgorithm>().ToString();
+            Enums.GetArbitraryValue<CipherMode>().ToString();
+            Enums.GetArbitraryValue<CipherPadding>().ToString();
 
             // TODO Flesh out the list of aliases
 
@@ -358,7 +358,7 @@ namespace Org.BouncyCastle.Security
             CipherAlgorithm cipherAlgorithm;
             try
             {
-                cipherAlgorithm = (CipherAlgorithm)Enums.GetEnumValue(typeof(CipherAlgorithm), algorithmName);
+                cipherAlgorithm = Enums.GetEnumValue<CipherAlgorithm>(algorithmName);
             }
             catch (ArgumentException)
             {
@@ -531,7 +531,7 @@ namespace Org.BouncyCastle.Security
                 {
                     try
                     {
-                        cipherPadding = (CipherPadding)Enums.GetEnumValue(typeof(CipherPadding), paddingName);
+                        cipherPadding = Enums.GetEnumValue<CipherPadding>(paddingName);
                     }
                     catch (ArgumentException)
                     {
@@ -632,7 +632,7 @@ namespace Org.BouncyCastle.Security
                 {
                     CipherMode cipherMode = modeName == ""
                         ? CipherMode.NONE
-                        : (CipherMode)Enums.GetEnumValue(typeof(CipherMode), modeName);
+                        : Enums.GetEnumValue<CipherMode>(modeName);
 
                     switch (cipherMode)
                     {
diff --git a/crypto/src/security/DigestUtilities.cs b/crypto/src/security/DigestUtilities.cs
index 3f3036c8c..8c175b056 100644
--- a/crypto/src/security/DigestUtilities.cs
+++ b/crypto/src/security/DigestUtilities.cs
@@ -51,7 +51,7 @@ namespace Org.BouncyCastle.Security
         static DigestUtilities()
         {
             // Signal to obfuscation tools not to change enum constants
-            ((DigestAlgorithm)Enums.GetArbitraryValue(typeof(DigestAlgorithm))).ToString();
+            Enums.GetArbitraryValue<DigestAlgorithm>().ToString();
 
             Aliases[PkcsObjectIdentifiers.MD2.Id] = "MD2";
             Aliases[PkcsObjectIdentifiers.MD4.Id] = "MD4";
@@ -199,8 +199,7 @@ namespace Org.BouncyCastle.Security
 
             try
             {
-                DigestAlgorithm digestAlgorithm = (DigestAlgorithm)Enums.GetEnumValue(
-                    typeof(DigestAlgorithm), mechanism);
+                DigestAlgorithm digestAlgorithm = Enums.GetEnumValue<DigestAlgorithm>(mechanism);
 
                 switch (digestAlgorithm)
                 {
diff --git a/crypto/src/security/WrapperUtilities.cs b/crypto/src/security/WrapperUtilities.cs
index 48ffe3a96..74e13f121 100644
--- a/crypto/src/security/WrapperUtilities.cs
+++ b/crypto/src/security/WrapperUtilities.cs
@@ -27,7 +27,7 @@ namespace Org.BouncyCastle.Security
         static WrapperUtilities()
         {
             // Signal to obfuscation tools not to change enum constants
-            ((WrapAlgorithm)Enums.GetArbitraryValue(typeof(WrapAlgorithm))).ToString();
+            Enums.GetArbitraryValue<WrapAlgorithm>().ToString();
 
             Algorithms[NistObjectIdentifiers.IdAes128Wrap.Id] = "AESWRAP";
             Algorithms[NistObjectIdentifiers.IdAes192Wrap.Id] = "AESWRAP";
@@ -56,8 +56,7 @@ namespace Org.BouncyCastle.Security
 
             try
             {
-                WrapAlgorithm wrapAlgorithm = (WrapAlgorithm)Enums.GetEnumValue(
-                    typeof(WrapAlgorithm), mechanism);
+                WrapAlgorithm wrapAlgorithm = Enums.GetEnumValue<WrapAlgorithm>(mechanism);
 
                 switch (wrapAlgorithm)
                 {
diff --git a/crypto/src/util/Enums.cs b/crypto/src/util/Enums.cs
index fb685e2a7..1034b5b7e 100644
--- a/crypto/src/util/Enums.cs
+++ b/crypto/src/util/Enums.cs
@@ -4,38 +4,43 @@ using Org.BouncyCastle.Utilities.Date;
 
 namespace Org.BouncyCastle.Utilities
 {
-    internal abstract class Enums
+    internal static class Enums
     {
-        internal static Enum GetEnumValue(Type enumType, string s)
+        internal static TEnum GetEnumValue<TEnum>(string s)
+            where TEnum : struct, Enum
         {
-            if (!enumType.IsEnum)
-                throw new ArgumentException("Not an enumeration type", nameof(enumType));
-
             // We only want to parse single named constants
             if (s.Length > 0 && char.IsLetter(s[0]) && s.IndexOf(',') < 0)
             {
                 s = s.Replace('-', '_');
                 s = s.Replace('/', '_');
 
-                return (Enum)Enum.Parse(enumType, s, false);
+#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                return Enum.Parse<TEnum>(s, false);
+#else
+                return (TEnum)Enum.Parse(typeof(TEnum), s, false);
+#endif
             }
 
             throw new ArgumentException();
         }
 
-        internal static Array GetEnumValues(Type enumType)
+        internal static TEnum[] GetEnumValues<TEnum>()
+            where TEnum : struct, Enum
         {
-            if (!enumType.IsEnum)
-                throw new ArgumentException("Not an enumeration type", nameof(enumType));
-
-            return Enum.GetValues(enumType);
+#if NET5_0_OR_GREATER
+            return Enum.GetValues<TEnum>();
+#else
+            return (TEnum[])Enum.GetValues(typeof(TEnum));
+#endif
         }
 
-        internal static Enum GetArbitraryValue(Type enumType)
+        internal static TEnum GetArbitraryValue<TEnum>()
+            where TEnum : struct, Enum
         {
-            Array values = GetEnumValues(enumType);
+            TEnum[] values = GetEnumValues<TEnum>();
             int pos = (int)(DateTimeUtilities.CurrentUnixMs() & int.MaxValue) % values.Length;
-            return (Enum)values.GetValue(pos);
+            return values[pos];
         }
     }
 }