summary refs log tree commit diff
path: root/crypto/src/openpgp/PgpPublicKeyRingBundle.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-28 15:37:25 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-28 15:37:25 +0700
commit3ce45e18811232677715a3bf4ba144a72b639942 (patch)
tree228c87fb260e125c66b34334cedb9b34b07a4dac /crypto/src/openpgp/PgpPublicKeyRingBundle.cs
parentGenerics migration in Crmf, Crypto, Math (diff)
downloadBouncyCastle.NET-ed25519-3ce45e18811232677715a3bf4ba144a72b639942.tar.xz
Generics migration in Ocsp, OpenPgp
Diffstat (limited to 'crypto/src/openpgp/PgpPublicKeyRingBundle.cs')
-rw-r--r--crypto/src/openpgp/PgpPublicKeyRingBundle.cs120
1 files changed, 42 insertions, 78 deletions
diff --git a/crypto/src/openpgp/PgpPublicKeyRingBundle.cs b/crypto/src/openpgp/PgpPublicKeyRingBundle.cs
index 23194d2d1..0de7e335e 100644
--- a/crypto/src/openpgp/PgpPublicKeyRingBundle.cs
+++ b/crypto/src/openpgp/PgpPublicKeyRingBundle.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
@@ -13,19 +13,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 	/// </remarks>
     public class PgpPublicKeyRingBundle
     {
-        private readonly IDictionary pubRings;
-        private readonly IList order;
+        private readonly IDictionary<long, PgpPublicKeyRing> m_pubRings;
+        private readonly IList<long> m_order;
 
-		private PgpPublicKeyRingBundle(
-            IDictionary	pubRings,
-            IList       order)
+		private PgpPublicKeyRingBundle(IDictionary<long, PgpPublicKeyRing> pubRings, IList<long> order)
         {
-            this.pubRings = pubRings;
-            this.order = order;
+            m_pubRings = pubRings;
+            m_order = order;
         }
 
-		public PgpPublicKeyRingBundle(
-            byte[] encoding)
+		public PgpPublicKeyRingBundle(byte[] encoding)
             : this(new MemoryStream(encoding, false))
         {
         }
@@ -34,51 +31,47 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// <param name="inputStream">Input stream containing data.</param>
 		/// <exception cref="IOException">If a problem parsing the stream occurs.</exception>
 		/// <exception cref="PgpException">If an object is encountered which isn't a PgpPublicKeyRing.</exception>
-		public PgpPublicKeyRingBundle(
-            Stream inputStream)
+		public PgpPublicKeyRingBundle(Stream inputStream)
 			: this(new PgpObjectFactory(inputStream).AllPgpObjects())
 		{
         }
 
-		public PgpPublicKeyRingBundle(
-            IEnumerable e)
+		public PgpPublicKeyRingBundle(IEnumerable<PgpObject> e)
         {
-			this.pubRings = Platform.CreateHashtable();
-			this.order = Platform.CreateArrayList();
+			m_pubRings = new Dictionary<long, PgpPublicKeyRing>();
+			m_order = new List<long>();
 
-			foreach (object obj in e)
+			foreach (var obj in e)
             {
                 // Marker packets must be ignored
                 if (obj is PgpMarker)
                     continue;
 
-                PgpPublicKeyRing pgpPub = obj as PgpPublicKeyRing;
-				if (pgpPub == null)
+				if (!(obj is PgpPublicKeyRing pgpPub))
 					throw new PgpException(Platform.GetTypeName(obj) + " found where PgpPublicKeyRing expected");
 
 				long key = pgpPub.GetPublicKey().KeyId;
-                pubRings.Add(key, pgpPub);
-				order.Add(key);
+                m_pubRings.Add(key, pgpPub);
+				m_order.Add(key);
             }
         }
 
 		/// <summary>Return the number of key rings in this collection.</summary>
         public int Count
         {
-			get { return order.Count; }
+			get { return m_order.Count; }
         }
 
 		/// <summary>Allow enumeration of the public key rings making up this collection.</summary>
-        public IEnumerable GetKeyRings()
+        public IEnumerable<PgpPublicKeyRing> GetKeyRings()
         {
-			return new EnumerableProxy(pubRings.Values);
+			return CollectionUtilities.Proxy(m_pubRings.Values);
         }
 
 		/// <summary>Allow enumeration of the key rings associated with the passed in userId.</summary>
 		/// <param name="userId">The user ID to be matched.</param>
 		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
-		public IEnumerable GetKeyRings(
-			string userId)
+		public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userId)
 		{
 			return GetKeyRings(userId, false, false);
 		}
@@ -87,9 +80,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// <param name="userId">The user ID to be matched.</param>
 		/// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param>
 		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
-        public IEnumerable GetKeyRings(
-            string	userId,
-            bool	matchPartial)
+        public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userId, bool matchPartial)
         {
 			return GetKeyRings(userId, matchPartial, false);
         }
@@ -99,12 +90,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param>
 		/// <param name="ignoreCase">If true, case is ignored in user ID comparisons.</param>
 		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
-		public IEnumerable GetKeyRings(
-			string	userId,
-			bool	matchPartial,
-			bool	ignoreCase)
+		public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userId, bool matchPartial, bool ignoreCase)
 		{
-			IList rings = Platform.CreateArrayList();
+			var rings = new List<PgpPublicKeyRing>();
 
 			if (ignoreCase)
 			{
@@ -138,22 +126,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 				}
 			}
 
-			return new EnumerableProxy(rings);
+			return CollectionUtilities.Proxy(rings);
 		}
 
 		/// <summary>Return the PGP public key associated with the given key id.</summary>
 		/// <param name="keyId">The ID of the public key to return.</param>
-        public PgpPublicKey GetPublicKey(
-            long keyId)
+        public PgpPublicKey GetPublicKey(long keyId)
         {
             foreach (PgpPublicKeyRing pubRing in GetKeyRings())
             {
                 PgpPublicKey pub = pubRing.GetPublicKey(keyId);
-
 				if (pub != null)
-                {
                     return pub;
-                }
             }
 
 			return null;
@@ -161,22 +145,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 
 		/// <summary>Return the public key ring which contains the key referred to by keyId</summary>
 		/// <param name="keyId">key ID to match against</param>
-        public PgpPublicKeyRing GetPublicKeyRing(
-            long keyId)
+        public PgpPublicKeyRing GetPublicKeyRing(long keyId)
         {
-            if (pubRings.Contains(keyId))
-            {
-                return (PgpPublicKeyRing)pubRings[keyId];
-            }
+			if (m_pubRings.TryGetValue(keyId, out var keyRing))
+				return keyRing;
 
 			foreach (PgpPublicKeyRing pubRing in GetKeyRings())
             {
-                PgpPublicKey pub = pubRing.GetPublicKey(keyId);
-
-                if (pub != null)
-                {
+                if (pubRing.GetPublicKey(keyId) != null)
                     return pubRing;
-                }
             }
 
 			return null;
@@ -186,8 +163,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// Return true if a key matching the passed in key ID is present, false otherwise.
 		/// </summary>
 		/// <param name="keyID">key ID to look for.</param>
-		public bool Contains(
-			long keyID)
+		public bool Contains(long keyID)
 		{
 			return GetPublicKey(keyID) != null;
 		}
@@ -195,22 +171,17 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		public byte[] GetEncoded()
         {
             MemoryStream bOut = new MemoryStream();
-
 			Encode(bOut);
-
 			return bOut.ToArray();
         }
 
-		public void Encode(
-            Stream outStr)
+		public void Encode(Stream outStr)
         {
 			BcpgOutputStream bcpgOut = BcpgOutputStream.Wrap(outStr);
 
-			foreach (long key in order)
+			foreach (long key in m_order)
             {
-                PgpPublicKeyRing sec = (PgpPublicKeyRing) pubRings[key];
-
-				sec.Encode(bcpgOut);
+                m_pubRings[key].Encode(bcpgOut);
             }
         }
 
@@ -222,22 +193,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// <param name="publicKeyRing">The key ring to be added.</param>
 		/// <returns>A new <c>PgpPublicKeyRingBundle</c> merging the current one with the passed in key ring.</returns>
 		/// <exception cref="ArgumentException">If the keyId for the passed in key ring is already present.</exception>
-        public static PgpPublicKeyRingBundle AddPublicKeyRing(
-            PgpPublicKeyRingBundle  bundle,
-            PgpPublicKeyRing        publicKeyRing)
+        public static PgpPublicKeyRingBundle AddPublicKeyRing(PgpPublicKeyRingBundle bundle,
+            PgpPublicKeyRing publicKeyRing)
         {
             long key = publicKeyRing.GetPublicKey().KeyId;
 
-			if (bundle.pubRings.Contains(key))
-            {
+			if (bundle.m_pubRings.ContainsKey(key))
                 throw new ArgumentException("Bundle already contains a key with a keyId for the passed in ring.");
-            }
 
-			IDictionary newPubRings = Platform.CreateHashtable(bundle.pubRings);
-            IList newOrder = Platform.CreateArrayList(bundle.order);
+			var newPubRings = new Dictionary<long, PgpPublicKeyRing>(bundle.m_pubRings);
+            var newOrder = new List<long>(bundle.m_order);
 
 			newPubRings[key] = publicKeyRing;
-
 			newOrder.Add(key);
 
 			return new PgpPublicKeyRingBundle(newPubRings, newOrder);
@@ -251,19 +218,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// <param name="publicKeyRing">The key ring to be removed.</param>
 		/// <returns>A new <c>PgpPublicKeyRingBundle</c> not containing the passed in key ring.</returns>
 		/// <exception cref="ArgumentException">If the keyId for the passed in key ring is not present.</exception>
-        public static PgpPublicKeyRingBundle RemovePublicKeyRing(
-            PgpPublicKeyRingBundle	bundle,
-            PgpPublicKeyRing		publicKeyRing)
+        public static PgpPublicKeyRingBundle RemovePublicKeyRing(PgpPublicKeyRingBundle bundle,
+            PgpPublicKeyRing publicKeyRing)
         {
             long key = publicKeyRing.GetPublicKey().KeyId;
 
-			if (!bundle.pubRings.Contains(key))
-            {
+			if (!bundle.m_pubRings.ContainsKey(key))
                 throw new ArgumentException("Bundle does not contain a key with a keyId for the passed in ring.");
-            }
 
-			IDictionary newPubRings = Platform.CreateHashtable(bundle.pubRings);
-            IList newOrder = Platform.CreateArrayList(bundle.order);
+			var newPubRings = new Dictionary<long, PgpPublicKeyRing>(bundle.m_pubRings);
+			var newOrder = new List<long>(bundle.m_order);
 
 			newPubRings.Remove(key);
 			newOrder.Remove(key);