From 435210f10fd927653ce8fbc04ec537ae5d8966b6 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 29 Jun 2022 14:15:10 +0700 Subject: Generics migration complete --- crypto/src/security/WrapperUtilities.cs | 68 ++++++++++++++------------------- 1 file changed, 28 insertions(+), 40 deletions(-) (limited to 'crypto/src/security/WrapperUtilities.cs') 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 { /// /// Utility class for creating IWrapper objects from their names/Oids /// - 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 Algorithms = + new Dictionary(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 -- cgit 1.4.1