From 35ad48901c94bf59499ff02a5cc9075e571f545a Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 25 Jun 2022 01:32:32 +0700 Subject: Refactoring --- .../src/asn1/cryptopro/GOST3410NamedParameters.cs | 91 +++++++++------------- .../cryptopro/GOST3410PublicKeyAlgParameters.cs | 13 +--- crypto/test/src/math/ec/test/FixedPointTest.cs | 10 +-- 3 files changed, 47 insertions(+), 67 deletions(-) diff --git a/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs b/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs index 66dba51d7..2d183a4f9 100644 --- a/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs +++ b/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs @@ -1,25 +1,14 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Math; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Asn1.CryptoPro { - /** - * table of the available named parameters for GOST 3410-94. - */ - public sealed class Gost3410NamedParameters + /// Registry of available named parameters for GOST 3410-94. + public static class Gost3410NamedParameters { - private Gost3410NamedParameters() - { - } - - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary parameters = Platform.CreateHashtable(); - private static readonly Gost3410ParamSetParameters cryptoProA = new Gost3410ParamSetParameters( 1024, new BigInteger("127021248288932417465907042777176443525787653508916535812817507265705031260985098497423188333483401180925999995120988934130659205614996724254121049274349357074920312769561451689224110579311248812610229678534638401693520013288995000362260684222750813532307004517341633685004541062586971416883686778842537820383"), @@ -65,59 +54,55 @@ namespace Org.BouncyCastle.Asn1.CryptoPro new BigInteger("133531813272720673433859519948319001217942375967847486899482359599369642528734712461590403327731821410328012529253871914788598993103310567744136196364803064721377826656898686468463277710150809401182608770201615324990468332931294920912776241137878030224355746606283971659376426832674269780880061631528163475887") ); - static Gost3410NamedParameters() - { - parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProA] = cryptoProA; - parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProB] = cryptoProB; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProC] = cryptoProC; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProD] = cryptoProD; - parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProXchA] = cryptoProXchA; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProXchB] = cryptoProXchA; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProXchC] = cryptoProXchA; + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary parameters = + new Dictionary(); - objIds["GostR3410-94-CryptoPro-A"] = CryptoProObjectIdentifiers.GostR3410x94CryptoProA; - objIds["GostR3410-94-CryptoPro-B"] = CryptoProObjectIdentifiers.GostR3410x94CryptoProB; - objIds["GostR3410-94-CryptoPro-XchA"] = CryptoProObjectIdentifiers.GostR3410x94CryptoProXchA; + private static void DefineParameters(string name, DerObjectIdentifier oid, + Gost3410ParamSetParameters parameterSet) + { + objIds.Add(name, oid); + parameters.Add(oid, parameterSet); } - /** - * return the GOST3410ParamSetParameters object for the given OID, null if it - * isn't present. - * - * @param oid an object identifier representing a named parameters, if present. - */ - public static Gost3410ParamSetParameters GetByOid( - DerObjectIdentifier oid) + static Gost3410NamedParameters() { - return (Gost3410ParamSetParameters) parameters[oid]; + DefineParameters("GostR3410-94-CryptoPro-A", CryptoProObjectIdentifiers.GostR3410x94CryptoProA, cryptoProA); + DefineParameters("GostR3410-94-CryptoPro-B", CryptoProObjectIdentifiers.GostR3410x94CryptoProB, cryptoProB); + DefineParameters("GostR3410-94-CryptoPro-XchA", CryptoProObjectIdentifiers.GostR3410x94CryptoProXchA, + cryptoProXchA); } - /** - * returns an enumeration containing the name strings for parameters - * contained in this structure. - */ - public static IEnumerable Names + /// Look up the for the parameter set with the given name. + /// + /// The name of the parameter set. + public static Gost3410ParamSetParameters GetByName(string name) { - get { return new EnumerableProxy(objIds.Keys); } + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOid(oid); } - public static Gost3410ParamSetParameters GetByName( - string name) + /// Look up the for the parameter set with the given + /// OID. + /// The OID for the parameter set. + public static Gost3410ParamSetParameters GetByOid(DerObjectIdentifier oid) { - DerObjectIdentifier oid = (DerObjectIdentifier) objIds[name]; - - if (oid != null) - { - return (Gost3410ParamSetParameters) parameters[oid]; - } + return parameters.TryGetValue(oid, out var parameterSet) ? parameterSet : null; + } - return null; + /// Look up the OID of the parameter set with the given name. + /// + /// The name of the parameter set. + public static DerObjectIdentifier GetOid(string name) + { + return objIds.TryGetValue(name, out var oid) ? oid : null; } - public static DerObjectIdentifier GetOid( - string name) + /// Enumerate the available parameter set names in this registry. + public static IEnumerable Names { - return (DerObjectIdentifier) objIds[name]; + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs index c6ea26204..40b69428d 100644 --- a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs +++ b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs @@ -1,7 +1,5 @@ using System; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.CryptoPro { public class Gost3410PublicKeyAlgParameters @@ -11,20 +9,17 @@ namespace Org.BouncyCastle.Asn1.CryptoPro private DerObjectIdentifier digestParamSet; private DerObjectIdentifier encryptionParamSet; - public static Gost3410PublicKeyAlgParameters GetInstance( - Asn1TaggedObject obj, - bool explicitly) + public static Gost3410PublicKeyAlgParameters GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { - return GetInstance(Asn1Sequence.GetInstance(obj, explicitly)); + return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); } - public static Gost3410PublicKeyAlgParameters GetInstance( - object obj) + public static Gost3410PublicKeyAlgParameters GetInstance(object obj) { if (obj == null || obj is Gost3410PublicKeyAlgParameters) return (Gost3410PublicKeyAlgParameters)obj; - return new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance((obj))); + return new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance(obj)); } public Gost3410PublicKeyAlgParameters( diff --git a/crypto/test/src/math/ec/test/FixedPointTest.cs b/crypto/test/src/math/ec/test/FixedPointTest.cs index 83e5fab8f..433f956da 100644 --- a/crypto/test/src/math/ec/test/FixedPointTest.cs +++ b/crypto/test/src/math/ec/test/FixedPointTest.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using NUnit.Framework; @@ -23,9 +23,9 @@ namespace Org.BouncyCastle.Math.EC.Tests { FixedPointCombMultiplier M = new FixedPointCombMultiplier(); - ArrayList names = new ArrayList(); - CollectionUtilities.AddRange(names, ECNamedCurveTable.Names); - CollectionUtilities.AddRange(names, CustomNamedCurves.Names); + var names = new List(); + names.AddRange(ECNamedCurveTable.Names); + names.AddRange(CustomNamedCurves.Names); ISet uniqNames = new HashSet(names); @@ -34,7 +34,7 @@ namespace Org.BouncyCastle.Math.EC.Tests X9ECParameters x9A = ECNamedCurveTable.GetByName(name); X9ECParameters x9B = CustomNamedCurves.GetByName(name); - X9ECParameters x9 = x9B != null ? x9B : x9A; + X9ECParameters x9 = x9B ?? x9A; for (int i = 0; i < TestsPerCurve; ++i) { -- cgit 1.4.1