From 70b47a3c76303a3af17c9ed635f7f1fa8a78ad18 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 1 Jul 2024 16:15:42 +0700 Subject: Refactoring in Asn1.Oiw --- crypto/src/asn1/oiw/ElGamalParameter.cs | 61 ++++++++++++++++++++------------ crypto/src/security/PrivateKeyFactory.cs | 3 +- crypto/src/security/PublicKeyFactory.cs | 3 +- crypto/test/src/test/ElGamalTest.cs | 2 +- 4 files changed, 41 insertions(+), 28 deletions(-) (limited to 'crypto') diff --git a/crypto/src/asn1/oiw/ElGamalParameter.cs b/crypto/src/asn1/oiw/ElGamalParameter.cs index b0a0ce348..4f1a8890c 100644 --- a/crypto/src/asn1/oiw/ElGamalParameter.cs +++ b/crypto/src/asn1/oiw/ElGamalParameter.cs @@ -7,39 +7,54 @@ namespace Org.BouncyCastle.Asn1.Oiw public class ElGamalParameter : Asn1Encodable { - internal DerInteger p, g; - - public ElGamalParameter( - BigInteger p, - BigInteger g) + public static ElGamalParameter GetInstance(object obj) { - this.p = new DerInteger(p); - this.g = new DerInteger(g); + if (obj == null) + return null; + if (obj is ElGamalParameter elGamalParameter) + return elGamalParameter; +#pragma warning disable CS0618 // Type or member is obsolete + return new ElGamalParameter(Asn1Sequence.GetInstance(obj)); +#pragma warning restore CS0618 // Type or member is obsolete } - public ElGamalParameter( - Asn1Sequence seq) + public static ElGamalParameter GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { - if (seq.Count != 2) - throw new ArgumentException("Wrong number of elements in sequence", "seq"); +#pragma warning disable CS0618 // Type or member is obsolete + return new ElGamalParameter(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); +#pragma warning restore CS0618 // Type or member is obsolete + } - p = DerInteger.GetInstance(seq[0]); - g = DerInteger.GetInstance(seq[1]); + public static ElGamalParameter GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) + { +#pragma warning disable CS0618 // Type or member is obsolete + return new ElGamalParameter(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); +#pragma warning restore CS0618 // Type or member is obsolete } - public BigInteger P - { - get { return p.PositiveValue; } - } + private readonly DerInteger m_p, m_g; - public BigInteger G - { - get { return g.PositiveValue; } - } + [Obsolete("Use 'GetInstance' instead")] + public ElGamalParameter(Asn1Sequence seq) + { + int count = seq.Count; + if (count != 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - public override Asn1Object ToAsn1Object() + m_p = DerInteger.GetInstance(seq[0]); + m_g = DerInteger.GetInstance(seq[1]); + } + + public ElGamalParameter(BigInteger p, BigInteger g) { - return new DerSequence(p, g); + m_p = new DerInteger(p); + m_g = new DerInteger(g); } + + public BigInteger P => m_p.PositiveValue; + + public BigInteger G => m_g.PositiveValue; + + public override Asn1Object ToAsn1Object() => new DerSequence(m_p, m_g); } } diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs index cf47eb272..670cebd7f 100644 --- a/crypto/src/security/PrivateKeyFactory.cs +++ b/crypto/src/security/PrivateKeyFactory.cs @@ -78,8 +78,7 @@ namespace Org.BouncyCastle.Security } else if (algOid.Equals(OiwObjectIdentifiers.ElGamalAlgorithm)) { - ElGamalParameter para = new ElGamalParameter( - Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object())); + ElGamalParameter para = ElGamalParameter.GetInstance(algID.Parameters); DerInteger derX = (DerInteger)keyInfo.ParsePrivateKey(); return new ElGamalPrivateKeyParameters( diff --git a/crypto/src/security/PublicKeyFactory.cs b/crypto/src/security/PublicKeyFactory.cs index 234847870..933af4841 100644 --- a/crypto/src/security/PublicKeyFactory.cs +++ b/crypto/src/security/PublicKeyFactory.cs @@ -102,8 +102,7 @@ namespace Org.BouncyCastle.Security } else if (algOid.Equals(OiwObjectIdentifiers.ElGamalAlgorithm)) { - ElGamalParameter para = new ElGamalParameter( - Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object())); + ElGamalParameter para = ElGamalParameter.GetInstance(algID.Parameters); DerInteger derY = (DerInteger)keyInfo.ParsePublicKey(); return new ElGamalPublicKeyParameters( diff --git a/crypto/test/src/test/ElGamalTest.cs b/crypto/test/src/test/ElGamalTest.cs index 293c0c37e..54e26a192 100644 --- a/crypto/test/src/test/ElGamalTest.cs +++ b/crypto/test/src/test/ElGamalTest.cs @@ -300,7 +300,7 @@ namespace Org.BouncyCastle.Tests // AlgorithmParameters a2 = AlgorithmParameters.getInstance("ElGamal"); // a2.init(encodeParams); - ElGamalParameter elP = new ElGamalParameter((Asn1Sequence) Asn1Object.FromByteArray(encodeParams)); + ElGamalParameter elP = ElGamalParameter.GetInstance(encodeParams); ElGamalParameters p2 = new ElGamalParameters(elP.P, elP.G); // a and a2 should be equivalent! -- cgit 1.4.1