diff options
Diffstat (limited to 'crypto/src/asn1/misc/IDEACBCPar.cs')
-rw-r--r-- | crypto/src/asn1/misc/IDEACBCPar.cs | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/crypto/src/asn1/misc/IDEACBCPar.cs b/crypto/src/asn1/misc/IDEACBCPar.cs index 9b73f5729..21eabea52 100644 --- a/crypto/src/asn1/misc/IDEACBCPar.cs +++ b/crypto/src/asn1/misc/IDEACBCPar.cs @@ -1,6 +1,5 @@ using System; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Misc @@ -8,42 +7,47 @@ namespace Org.BouncyCastle.Asn1.Misc public class IdeaCbcPar : Asn1Encodable { - internal Asn1OctetString iv; + public static IdeaCbcPar GetInstance(object o) + { + if (o == null) + return null; + if (o is IdeaCbcPar ideaCbcPar) + return ideaCbcPar; + return new IdeaCbcPar(Asn1Sequence.GetInstance(o)); + } + + public static IdeaCbcPar GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return new IdeaCbcPar(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } + + private readonly Asn1OctetString m_iv; - public static IdeaCbcPar GetInstance( - object o) + private IdeaCbcPar(Asn1Sequence seq) { - if (o is IdeaCbcPar) - { - return (IdeaCbcPar) o; - } + int count = seq.Count, pos = 0; + if (count < 0 || count > 1) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - if (o is Asn1Sequence) - { - return new IdeaCbcPar((Asn1Sequence) o); - } + m_iv = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional); - throw new ArgumentException("unknown object in IDEACBCPar factory"); + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); } - public IdeaCbcPar( - byte[] iv) + public IdeaCbcPar() + : this(iv: null) { - this.iv = new DerOctetString(iv); } - private IdeaCbcPar( - Asn1Sequence seq) + public IdeaCbcPar(byte[] iv) { - if (seq.Count == 1) - { - iv = (Asn1OctetString) seq[0]; - } + m_iv = iv == null ? null : new DerOctetString(iv); } - public Asn1OctetString IV => iv; + public Asn1OctetString IV => m_iv; - public byte[] GetIV() => Arrays.Clone(iv.GetOctets()); + public byte[] GetIV() => Arrays.Clone(m_iv?.GetOctets()); /** * Produce an object suitable for an Asn1OutputStream. @@ -55,9 +59,9 @@ namespace Org.BouncyCastle.Asn1.Misc */ public override Asn1Object ToAsn1Object() { - return iv != null - ? new DerSequence(iv) - : DerSequence.Empty; + return m_iv == null + ? DerSequence.Empty + : new DerSequence(m_iv); } } } |