summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-28 00:47:21 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-28 00:47:21 +0700
commitbf718b4e64a757a8a5442c935cc862b8f231c103 (patch)
tree7302f2840266b6b38855c7d38b83e473d359de51
parentFix NPE (diff)
downloadBouncyCastle.NET-ed25519-bf718b4e64a757a8a5442c935cc862b8f231c103.tar.xz
Generics migration in Bcpg, Bzip2, Cmp
-rw-r--r--crypto/src/bcpg/ArmoredInputStream.cs7
-rw-r--r--crypto/src/bcpg/ArmoredOutputStream.cs75
-rw-r--r--crypto/src/bcpg/SignaturePacket.cs33
-rw-r--r--crypto/src/bcpg/UserAttributePacket.cs14
-rw-r--r--crypto/src/bzip2/CBZip2OutputStream.cs12
-rw-r--r--crypto/src/cmp/CertificateConfirmationContentBuilder.cs12
-rw-r--r--crypto/src/cmp/ProtectedPkiMessageBuilder.cs37
7 files changed, 77 insertions, 113 deletions
diff --git a/crypto/src/bcpg/ArmoredInputStream.cs b/crypto/src/bcpg/ArmoredInputStream.cs
index cb5c2f91f..224ef7d28 100644
--- a/crypto/src/bcpg/ArmoredInputStream.cs
+++ b/crypto/src/bcpg/ArmoredInputStream.cs
@@ -1,5 +1,4 @@
-using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 using System.Text;
 
@@ -114,7 +113,7 @@ namespace Org.BouncyCastle.Bcpg
         bool        newLineFound = false;
         bool        clearText = false;
         bool        restart = false;
-        IList       headerList = Platform.CreateArrayList();
+        IList<string> headerList = new List<string>();
         int         lastC = 0;
 		bool		isEndOfStream;
 
@@ -158,7 +157,7 @@ namespace Org.BouncyCastle.Bcpg
             int		last = 0;
             bool	headerFound = false;
 
-            headerList = Platform.CreateArrayList();
+            headerList = new List<string>();
 
 			//
             // if restart we already have a header
diff --git a/crypto/src/bcpg/ArmoredOutputStream.cs b/crypto/src/bcpg/ArmoredOutputStream.cs
index 919325a73..a3d86429c 100644
--- a/crypto/src/bcpg/ArmoredOutputStream.cs
+++ b/crypto/src/bcpg/ArmoredOutputStream.cs
@@ -1,15 +1,9 @@
-using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
-using System.Reflection;
-using System.Text;
-
-#if PORTABLE
-using System.Linq;
-#endif
 
 using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Collections;
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Bcpg
@@ -105,24 +99,24 @@ namespace Org.BouncyCastle.Bcpg
 
         private static readonly string Version = "BCPG C# v" + typeof(ArmoredOutputStream).Assembly.GetName().Version;
 
-        private readonly IDictionary headers;
+        private readonly IDictionary<string, IList<string>> m_headers;
 
         public ArmoredOutputStream(Stream outStream)
         {
             this.outStream = outStream;
-            this.headers = Platform.CreateHashtable(1);
+            this.m_headers = new Dictionary<string, IList<string>>(1);
             SetHeader(HeaderVersion, Version);
         }
 
-        public ArmoredOutputStream(Stream outStream, IDictionary headers)
+        public ArmoredOutputStream(Stream outStream, IDictionary<string, string> headers)
             : this(outStream)
         {
-            foreach (string header in headers.Keys)
+            foreach (var header in headers)
             {
-                IList headerList = Platform.CreateArrayList(1);
-                headerList.Add(headers[header]);
+                var headerList = new List<string>(1);
+                headerList.Add(header.Value);
 
-                this.headers[header] = headerList;
+                m_headers[header.Key] = headerList;
             }
         }
 
@@ -136,22 +130,21 @@ namespace Org.BouncyCastle.Bcpg
         {
             if (val == null)
             {
-                this.headers.Remove(name);
+                this.m_headers.Remove(name);
+                return;
+            }
+
+            if (m_headers.TryGetValue(name, out var valueList))
+            {
+                valueList.Clear();
             }
             else
             {
-                IList valueList = (IList)headers[name];
-                if (valueList == null)
-                {
-                    valueList = Platform.CreateArrayList(1);
-                    this.headers[name] = valueList;
-                }
-                else
-                {
-                    valueList.Clear();
-                }
-                valueList.Add(val);
+                valueList = new List<string>(1);
+                m_headers[name] = valueList;
             }
+
+            valueList.Add(val);
         }
 
         /**
@@ -166,12 +159,12 @@ namespace Org.BouncyCastle.Bcpg
             if (val == null || name == null)
                 return;
 
-            IList valueList = (IList)headers[name];
-            if (valueList == null)
+            if (!m_headers.TryGetValue(name, out var valueList))
             {
-                valueList = Platform.CreateArrayList(1);
-                this.headers[name] = valueList;
+                valueList = new List<string>(1);
+                m_headers[name] = valueList;
             }
+
             valueList.Add(val);
         }
 
@@ -180,13 +173,13 @@ namespace Org.BouncyCastle.Bcpg
          */
         public void ResetHeaders()
         {
-            IList versions = (IList)headers[HeaderVersion];
+            var versions = CollectionUtilities.GetValueOrNull(m_headers, HeaderVersion);
 
-            headers.Clear();
+            m_headers.Clear();
 
             if (versions != null)
             {
-                headers[HeaderVersion] = versions;
+                m_headers[HeaderVersion] = versions;
             }
         }
 
@@ -297,21 +290,17 @@ namespace Org.BouncyCastle.Bcpg
 
                 DoWrite(headerStart + type + headerTail + nl);
 
+                if (m_headers.TryGetValue(HeaderVersion, out var versionHeaders))
                 {
-                    IList versionHeaders = (IList)headers[HeaderVersion];
-                    if (versionHeaders != null)
-                    {
-                        WriteHeaderEntry(HeaderVersion, versionHeaders[0].ToString());
-                    }
+                    WriteHeaderEntry(HeaderVersion, versionHeaders[0]);
                 }
 
-                foreach (DictionaryEntry de in headers)
+                foreach (var de in m_headers)
                 {
-                    string k = (string)de.Key;
+                    string k = de.Key;
                     if (k != HeaderVersion)
                     {
-                        IList values = (IList)de.Value;
-                        foreach (string v in values)
+                        foreach (string v in de.Value)
                         {
                             WriteHeaderEntry(k, v);
                         }
diff --git a/crypto/src/bcpg/SignaturePacket.cs b/crypto/src/bcpg/SignaturePacket.cs
index 9a664f902..ca11ba9f4 100644
--- a/crypto/src/bcpg/SignaturePacket.cs
+++ b/crypto/src/bcpg/SignaturePacket.cs
@@ -1,9 +1,8 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Bcpg.Sig;
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Date;
 using Org.BouncyCastle.Utilities.IO;
 
@@ -68,29 +67,26 @@ namespace Org.BouncyCastle.Bcpg
                 SignatureSubpacketsParser sIn = new SignatureSubpacketsParser(
                     new MemoryStream(hashed, false));
 
-				IList v = Platform.CreateArrayList();
+                var v = new List<SignatureSubpacket>();
+
 				SignatureSubpacket sub;
 				while ((sub = sIn.ReadPacket()) != null)
                 {
                     v.Add(sub);
                 }
 
-				hashedData = new SignatureSubpacket[v.Count];
+                hashedData = v.ToArray();
 
-				for (int i = 0; i != hashedData.Length; i++)
+				foreach (var p in hashedData)
                 {
-                    SignatureSubpacket p = (SignatureSubpacket)v[i];
-                    if (p is IssuerKeyId)
+                    if (p is IssuerKeyId issuerKeyId)
                     {
-                        keyId = ((IssuerKeyId)p).KeyId;
+                        keyId = issuerKeyId.KeyId;
                     }
-                    else if (p is SignatureCreationTime)
+                    else if (p is SignatureCreationTime sigCreationTime)
                     {
-                        creationTime = DateTimeUtilities.DateTimeToUnixMs(
-							((SignatureCreationTime)p).GetTime());
+                        creationTime = DateTimeUtilities.DateTimeToUnixMs(sigCreationTime.GetTime());
                     }
-
-					hashedData[i] = p;
                 }
 
 				int unhashedLength = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();
@@ -107,17 +103,14 @@ namespace Org.BouncyCastle.Bcpg
                     v.Add(sub);
                 }
 
-				unhashedData = new SignatureSubpacket[v.Count];
+                unhashedData = v.ToArray();
 
-				for (int i = 0; i != unhashedData.Length; i++)
+				foreach (var p in unhashedData)
                 {
-                    SignatureSubpacket p = (SignatureSubpacket)v[i];
-                    if (p is IssuerKeyId)
+                    if (p is IssuerKeyId issuerKeyId)
                     {
-                        keyId = ((IssuerKeyId)p).KeyId;
+                        keyId = issuerKeyId.KeyId;
                     }
-
-					unhashedData[i] = p;
                 }
             }
             else
diff --git a/crypto/src/bcpg/UserAttributePacket.cs b/crypto/src/bcpg/UserAttributePacket.cs
index 20e3598ab..861f62f5d 100644
--- a/crypto/src/bcpg/UserAttributePacket.cs
+++ b/crypto/src/bcpg/UserAttributePacket.cs
@@ -1,9 +1,6 @@
-using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Bcpg
 {
     /**
@@ -20,18 +17,13 @@ namespace Org.BouncyCastle.Bcpg
             UserAttributeSubpacketsParser sIn = new UserAttributeSubpacketsParser(bcpgIn);
             UserAttributeSubpacket sub;
 
-            IList v = Platform.CreateArrayList();
+            var v = new List<UserAttributeSubpacket>();
             while ((sub = sIn.ReadPacket()) != null)
             {
                 v.Add(sub);
             }
 
-            subpackets = new UserAttributeSubpacket[v.Count];
-
-            for (int i = 0; i != subpackets.Length; i++)
-            {
-                subpackets[i] = (UserAttributeSubpacket)v[i];
-            }
+            subpackets = v.ToArray();
         }
 
         public UserAttributePacket(
diff --git a/crypto/src/bzip2/CBZip2OutputStream.cs b/crypto/src/bzip2/CBZip2OutputStream.cs
index 045506931..38966efd4 100644
--- a/crypto/src/bzip2/CBZip2OutputStream.cs
+++ b/crypto/src/bzip2/CBZip2OutputStream.cs
@@ -23,7 +23,7 @@
  */
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 
@@ -254,7 +254,7 @@ namespace Org.BouncyCastle.Bzip2
         private readonly int allowableBlockSize;
 
         bool blockRandomised;
-        private readonly IList blocksortStack = Platform.CreateArrayList();
+        private readonly IList<StackElem> blocksortStack = new List<StackElem>();
 
         int bsBuff;
         int bsLivePos;
@@ -1043,12 +1043,12 @@ namespace Org.BouncyCastle.Bzip2
             internal int dd;
         }
 
-        private static void PushStackElem(IList stack, int stackCount, int ll, int hh, int dd)
+        private static void PushStackElem(IList<StackElem> stack, int stackCount, int ll, int hh, int dd)
         {
             StackElem stackElem;
             if (stackCount < stack.Count)
             {
-                stackElem = (StackElem)stack[stackCount];
+                stackElem = stack[stackCount];
             }
             else
             {
@@ -1065,7 +1065,7 @@ namespace Org.BouncyCastle.Bzip2
         {
             int unLo, unHi, ltLo, gtHi, n, m;
 
-            IList stack = blocksortStack;
+            var stack = blocksortStack;
             int stackCount = 0;
             StackElem stackElem;
 
@@ -1081,7 +1081,7 @@ namespace Org.BouncyCastle.Bzip2
                     if (stackCount < 1 || (workDone > workLimit && firstAttempt))
                         return;
 
-                    stackElem = (StackElem)stack[--stackCount];
+                    stackElem = stack[--stackCount];
                     lo = stackElem.ll;
                     hi = stackElem.hh;
                     d = stackElem.dd;
diff --git a/crypto/src/cmp/CertificateConfirmationContentBuilder.cs b/crypto/src/cmp/CertificateConfirmationContentBuilder.cs
index 611fa4449..94f5eda8b 100644
--- a/crypto/src/cmp/CertificateConfirmationContentBuilder.cs
+++ b/crypto/src/cmp/CertificateConfirmationContentBuilder.cs
@@ -1,14 +1,12 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Cmp;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Cms;
-using Org.BouncyCastle.Crypto.IO;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Security;
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.X509;
 
 namespace Org.BouncyCastle.Cmp
@@ -18,8 +16,8 @@ namespace Org.BouncyCastle.Cmp
         private static readonly DefaultSignatureAlgorithmIdentifierFinder sigAlgFinder = new DefaultSignatureAlgorithmIdentifierFinder();
 
         private readonly DefaultDigestAlgorithmIdentifierFinder digestAlgFinder;
-        private readonly IList acceptedCerts = Platform.CreateArrayList();
-        private readonly IList acceptedReqIds = Platform.CreateArrayList();
+        private readonly IList<X509Certificate> acceptedCerts = new List<X509Certificate>();
+        private readonly IList<BigInteger> acceptedReqIds = new List<BigInteger>();
 
         public CertificateConfirmationContentBuilder()
             : this(new DefaultDigestAlgorithmIdentifierFinder())
@@ -44,8 +42,8 @@ namespace Org.BouncyCastle.Cmp
             Asn1EncodableVector v = new Asn1EncodableVector();
             for (int i = 0; i != acceptedCerts.Count; i++)
             {
-                X509Certificate cert = (X509Certificate)acceptedCerts[i];
-                BigInteger reqId = (BigInteger)acceptedReqIds[i];
+                X509Certificate cert = acceptedCerts[i];
+                BigInteger reqId = acceptedReqIds[i];
 
 
                 AlgorithmIdentifier algorithmIdentifier = sigAlgFinder.Find(cert.SigAlgName);
diff --git a/crypto/src/cmp/ProtectedPkiMessageBuilder.cs b/crypto/src/cmp/ProtectedPkiMessageBuilder.cs
index 5939e92df..269910cc6 100644
--- a/crypto/src/cmp/ProtectedPkiMessageBuilder.cs
+++ b/crypto/src/cmp/ProtectedPkiMessageBuilder.cs
@@ -1,12 +1,11 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Cmp;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Crypto.Operators;
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.X509;
 
 namespace Org.BouncyCastle.Cmp
@@ -15,8 +14,8 @@ namespace Org.BouncyCastle.Cmp
     {
         private PkiHeaderBuilder hdrBuilBuilder;
         private PkiBody body;
-        private IList generalInfos = Platform.CreateArrayList();
-        private IList extraCerts = Platform.CreateArrayList();
+        private List<InfoTypeAndValue> generalInfos = new List<InfoTypeAndValue>();
+        private List<X509Certificate> extraCerts = new List<X509Certificate>();
 
         public ProtectedPkiMessageBuilder(GeneralName sender, GeneralName recipient)
             : this(PkiHeader.CMP_2000, sender, recipient)
@@ -123,13 +122,7 @@ namespace Org.BouncyCastle.Cmp
             hdrBuilBuilder.SetProtectionAlg(algorithmIdentifier);
             if (generalInfos.Count > 0)
             {
-                InfoTypeAndValue[] genInfos = new InfoTypeAndValue[generalInfos.Count];
-                for (int t = 0; t < genInfos.Length; t++)
-                {
-                    genInfos[t] = (InfoTypeAndValue)generalInfos[t];
-                }
-
-                hdrBuilBuilder.SetGeneralInfo(genInfos);
+                hdrBuilBuilder.SetGeneralInfo(generalInfos.ToArray());
             }
         }
 
@@ -140,8 +133,8 @@ namespace Org.BouncyCastle.Cmp
                 CmpCertificate[] cmpCertificates = new CmpCertificate[extraCerts.Count];
                 for (int i = 0; i < cmpCertificates.Length; i++)
                 {
-                    byte[] cert = ((X509Certificate)extraCerts[i]).GetEncoded();
-                    cmpCertificates[i] = CmpCertificate.GetInstance((Asn1Sequence.FromByteArray(cert)));
+                    byte[] cert = extraCerts[i].GetEncoded();
+                    cmpCertificates[i] = CmpCertificate.GetInstance(Asn1Object.FromByteArray(cert));
                 }
 
                 return new ProtectedPkiMessage(new PkiMessage(header, body, protection, cmpCertificates));
@@ -152,24 +145,24 @@ namespace Org.BouncyCastle.Cmp
 
         private byte[] CalculateSignature(IStreamCalculator signer, PkiHeader header, PkiBody body)
         {
-            Asn1EncodableVector avec = new Asn1EncodableVector();
+            Asn1EncodableVector avec = new Asn1EncodableVector(2);
             avec.Add(header);
             avec.Add(body);
-            byte[] encoded = new DerSequence(avec).GetEncoded();
-            signer.Stream.Write(encoded, 0, encoded.Length);
+
+            new DerSequence(avec).EncodeTo(signer.Stream);
             object result = signer.GetResult();
 
-            if (result is DefaultSignatureResult)
+            if (result is DefaultSignatureResult sigResult)
             {
-                return ((DefaultSignatureResult)result).Collect();
+                return sigResult.Collect();
             }
-            else if (result is IBlockResult)
+            else if (result is IBlockResult blockResult)
             {
-                return ((IBlockResult)result).Collect();
+                return blockResult.Collect();
             }
-            else if (result is byte[])
+            else if (result is byte[] bytesResult)
             {
-                return (byte[])result;
+                return bytesResult;
             }
 
             throw new InvalidOperationException("result is not byte[] or DefaultSignatureResult");