diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-25 01:32:32 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-25 01:32:32 +0700 |
commit | 35ad48901c94bf59499ff02a5cc9075e571f545a (patch) | |
tree | bb5a8560a2bb12ce240dbe556264c3da4eaf8b02 /crypto/src/asn1 | |
parent | Replace Dump program with static utility method (diff) | |
download | BouncyCastle.NET-ed25519-35ad48901c94bf59499ff02a5cc9075e571f545a.tar.xz |
Refactoring
Diffstat (limited to 'crypto/src/asn1')
-rw-r--r-- | crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs | 91 | ||||
-rw-r--r-- | crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs | 13 |
2 files changed, 42 insertions, 62 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 + /// <summary>Registry of available named parameters for GOST 3410-94.</summary> + 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<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, Gost3410ParamSetParameters> parameters = + new Dictionary<DerObjectIdentifier, Gost3410ParamSetParameters>(); - 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 + /// <summary>Look up the <see cref="Gost3410ParamSetParameters"/> for the parameter set with the given name. + /// </summary> + /// <param name="name">The name of the parameter set.</param> + 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) + /// <summary>Look up the <see cref="Gost3410ParamSetParameters"/> for the parameter set with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the parameter set.</param> + 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; + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the parameter set with the given name. + /// </summary> + /// <param name="name">The name of the parameter set.</param> + public static DerObjectIdentifier GetOid(string name) + { + return objIds.TryGetValue(name, out var oid) ? oid : null; } - public static DerObjectIdentifier GetOid( - string name) + /// <summary>Enumerate the available parameter set names in this registry.</summary> + public static IEnumerable<string> 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( |