summary refs log tree commit diff
path: root/crypto/src/security/WrapperUtilities.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:15:10 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:15:10 +0700
commit435210f10fd927653ce8fbc04ec537ae5d8966b6 (patch)
tree27b6ed1c029db271c3429ac57629d7f0156c5fed /crypto/src/security/WrapperUtilities.cs
parentRefactoring around Platform (diff)
downloadBouncyCastle.NET-ed25519-435210f10fd927653ce8fbc04ec537ae5d8966b6.tar.xz
Generics migration complete
Diffstat (limited to 'crypto/src/security/WrapperUtilities.cs')
-rw-r--r--crypto/src/security/WrapperUtilities.cs68
1 files changed, 28 insertions, 40 deletions
diff --git a/crypto/src/security/WrapperUtilities.cs b/crypto/src/security/WrapperUtilities.cs
index c57632081..983ff824c 100644
--- a/crypto/src/security/WrapperUtilities.cs
+++ b/crypto/src/security/WrapperUtilities.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Kisa;
@@ -9,61 +9,50 @@ using Org.BouncyCastle.Asn1.Pkcs;
 using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Crypto.Engines;
 using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Collections;
 
 namespace Org.BouncyCastle.Security
 {
     /// <remarks>
     ///  Utility class for creating IWrapper objects from their names/Oids
     /// </remarks>
-    public sealed class WrapperUtilities
+    public static class WrapperUtilities
     {
         private enum WrapAlgorithm { AESWRAP, CAMELLIAWRAP, DESEDEWRAP, RC2WRAP, SEEDWRAP,
             DESEDERFC3211WRAP, AESRFC3211WRAP, CAMELLIARFC3211WRAP };
 
-        private WrapperUtilities()
-        {
-        }
-
-        private static readonly IDictionary algorithms = Platform.CreateHashtable();
-        //private static readonly IDictionary oids = Platform.CreateHashtable();
+        private static readonly IDictionary<string, string> Algorithms =
+            new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
 
         static WrapperUtilities()
         {
             // Signal to obfuscation tools not to change enum constants
             ((WrapAlgorithm)Enums.GetArbitraryValue(typeof(WrapAlgorithm))).ToString();
 
-            algorithms[NistObjectIdentifiers.IdAes128Wrap.Id] = "AESWRAP";
-            algorithms[NistObjectIdentifiers.IdAes192Wrap.Id] = "AESWRAP";
-            algorithms[NistObjectIdentifiers.IdAes256Wrap.Id] = "AESWRAP";
+            Algorithms[NistObjectIdentifiers.IdAes128Wrap.Id] = "AESWRAP";
+            Algorithms[NistObjectIdentifiers.IdAes192Wrap.Id] = "AESWRAP";
+            Algorithms[NistObjectIdentifiers.IdAes256Wrap.Id] = "AESWRAP";
 
-            algorithms[NttObjectIdentifiers.IdCamellia128Wrap.Id] = "CAMELLIAWRAP";
-            algorithms[NttObjectIdentifiers.IdCamellia192Wrap.Id] = "CAMELLIAWRAP";
-            algorithms[NttObjectIdentifiers.IdCamellia256Wrap.Id] = "CAMELLIAWRAP";
+            Algorithms[NttObjectIdentifiers.IdCamellia128Wrap.Id] = "CAMELLIAWRAP";
+            Algorithms[NttObjectIdentifiers.IdCamellia192Wrap.Id] = "CAMELLIAWRAP";
+            Algorithms[NttObjectIdentifiers.IdCamellia256Wrap.Id] = "CAMELLIAWRAP";
 
-            algorithms[PkcsObjectIdentifiers.IdAlgCms3DesWrap.Id] = "DESEDEWRAP";
-            algorithms["TDEAWRAP"] = "DESEDEWRAP";
+            Algorithms[PkcsObjectIdentifiers.IdAlgCms3DesWrap.Id] = "DESEDEWRAP";
+            Algorithms["TDEAWRAP"] = "DESEDEWRAP";
 
-            algorithms[PkcsObjectIdentifiers.IdAlgCmsRC2Wrap.Id] = "RC2WRAP";
+            Algorithms[PkcsObjectIdentifiers.IdAlgCmsRC2Wrap.Id] = "RC2WRAP";
 
-            algorithms[KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap.Id] = "SEEDWRAP";
+            Algorithms[KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap.Id] = "SEEDWRAP";
         }
 
-        public static IWrapper GetWrapper(
-            DerObjectIdentifier oid)
+        public static IWrapper GetWrapper(DerObjectIdentifier oid)
         {
             return GetWrapper(oid.Id);
         }
 
-        public static IWrapper GetWrapper(
-            string algorithm)
+        public static IWrapper GetWrapper(string algorithm)
         {
-            string upper = Platform.ToUpperInvariant(algorithm);
-            string mechanism = (string)algorithms[upper];
-
-            if (mechanism == null)
-            {
-                mechanism = upper;
-            }
+            string mechanism = CollectionUtilities.GetValueOrKey(Algorithms, algorithm).ToUpperInvariant();
 
             try
             {
@@ -72,14 +61,14 @@ namespace Org.BouncyCastle.Security
 
                 switch (wrapAlgorithm)
                 {
-                    case WrapAlgorithm.AESWRAP:				return new AesWrapEngine();
-                    case WrapAlgorithm.CAMELLIAWRAP:		return new CamelliaWrapEngine();
-                    case WrapAlgorithm.DESEDEWRAP:			return new DesEdeWrapEngine();
-                    case WrapAlgorithm.RC2WRAP:				return new RC2WrapEngine();
-                    case WrapAlgorithm.SEEDWRAP:			return new SeedWrapEngine();
-                    case WrapAlgorithm.DESEDERFC3211WRAP:	return new Rfc3211WrapEngine(new DesEdeEngine());
-                    case WrapAlgorithm.AESRFC3211WRAP:		return new Rfc3211WrapEngine(new AesEngine());
-                    case WrapAlgorithm.CAMELLIARFC3211WRAP:	return new Rfc3211WrapEngine(new CamelliaEngine());
+                case WrapAlgorithm.AESWRAP:				return new AesWrapEngine();
+                case WrapAlgorithm.CAMELLIAWRAP:		return new CamelliaWrapEngine();
+                case WrapAlgorithm.DESEDEWRAP:			return new DesEdeWrapEngine();
+                case WrapAlgorithm.RC2WRAP:				return new RC2WrapEngine();
+                case WrapAlgorithm.SEEDWRAP:			return new SeedWrapEngine();
+                case WrapAlgorithm.DESEDERFC3211WRAP:	return new Rfc3211WrapEngine(new DesEdeEngine());
+                case WrapAlgorithm.AESRFC3211WRAP:		return new Rfc3211WrapEngine(new AesEngine());
+                case WrapAlgorithm.CAMELLIARFC3211WRAP:	return new Rfc3211WrapEngine(new CamelliaEngine());
                 }
             }
             catch (ArgumentException)
@@ -95,10 +84,9 @@ namespace Org.BouncyCastle.Security
             throw new SecurityUtilityException("Wrapper " + algorithm + " not recognised.");
         }
 
-        public static string GetAlgorithmName(
-            DerObjectIdentifier oid)
+        public static string GetAlgorithmName(DerObjectIdentifier oid)
         {
-            return (string) algorithms[oid.Id];
+            return CollectionUtilities.GetValueOrNull(Algorithms, oid.Id);
         }
 
         private class BufferedCipherWrapper