summary refs log tree commit diff
path: root/crypto/src/asn1/pkcs/RC2CBCParameter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/pkcs/RC2CBCParameter.cs')
-rw-r--r--crypto/src/asn1/pkcs/RC2CBCParameter.cs79
1 files changed, 34 insertions, 45 deletions
diff --git a/crypto/src/asn1/pkcs/RC2CBCParameter.cs b/crypto/src/asn1/pkcs/RC2CBCParameter.cs
index c1f09a088..f38695f50 100644
--- a/crypto/src/asn1/pkcs/RC2CBCParameter.cs
+++ b/crypto/src/asn1/pkcs/RC2CBCParameter.cs
@@ -8,67 +8,56 @@ namespace Org.BouncyCastle.Asn1.Pkcs
     public class RC2CbcParameter
         : Asn1Encodable
     {
-        internal DerInteger			version;
-        internal Asn1OctetString	iv;
-
-		public static RC2CbcParameter GetInstance(
-            object obj)
+		public static RC2CbcParameter GetInstance(object obj)
         {
-            if (obj is Asn1Sequence)
-            {
-                return new RC2CbcParameter((Asn1Sequence) obj);
-            }
-
-			throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
+            if (obj == null)
+                return null;
+            if (obj is RC2CbcParameter rc2CbcParameter)
+                return rc2CbcParameter;
+            return new RC2CbcParameter(Asn1Sequence.GetInstance(obj));
 		}
 
-		public RC2CbcParameter(
-            byte[] iv)
-        {
-            this.iv = new DerOctetString(iv);
-        }
+        private readonly DerInteger m_version;
+        private readonly Asn1OctetString m_iv;
 
-		public RC2CbcParameter(
-            int		parameterVersion,
-            byte[]	iv)
+        private RC2CbcParameter(Asn1Sequence seq)
         {
-            this.version = new DerInteger(parameterVersion);
-            this.iv = new DerOctetString(iv);
-        }
+            int count = seq.Count, pos = 0;
+            if (count < 1 || count > 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-		private RC2CbcParameter(
-            Asn1Sequence seq)
-        {
-            if (seq.Count == 1)
-            {
-                iv = (Asn1OctetString)seq[0];
-            }
-            else
-            {
-                version = (DerInteger)seq[0];
-                iv = (Asn1OctetString)seq[1];
-            }
+            m_version = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional);
+            m_iv = Asn1OctetString.GetInstance(seq[pos++]);
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-		public BigInteger RC2ParameterVersion
+        public RC2CbcParameter(byte[] iv)
         {
-            get
-            {
-				return version == null ? null : version.Value;
-            }
+            m_version = null;
+            m_iv = new DerOctetString(iv);
         }
 
-		public byte[] GetIV()
+		public RC2CbcParameter(int parameterVersion, byte[] iv)
         {
-			return Arrays.Clone(iv.GetOctets());
+            m_version = new DerInteger(parameterVersion);
+            m_iv = new DerOctetString(iv);
         }
 
+        public BigInteger RC2ParameterVersion => m_version?.Value;
+
+        public DerInteger RC2ParameterVersionData => m_version;
+
+        public Asn1OctetString IV => m_iv;
+
+        public byte[] GetIV() => Arrays.Clone(m_iv.GetOctets());
+
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(2);
-            v.AddOptional(version);
-            v.Add(iv);
-            return new DerSequence(v);
+            return m_version == null
+                ?  new DerSequence(m_iv)
+                :  new DerSequence(m_version, m_iv);
         }
     }
 }