From 3ce45e18811232677715a3bf4ba144a72b639942 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 28 Jun 2022 15:37:25 +0700 Subject: Generics migration in Ocsp, OpenPgp --- crypto/src/openpgp/PgpPublicKeyRingBundle.cs | 120 ++++++++++----------------- 1 file changed, 42 insertions(+), 78 deletions(-) (limited to 'crypto/src/openpgp/PgpPublicKeyRingBundle.cs') 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 /// public class PgpPublicKeyRingBundle { - private readonly IDictionary pubRings; - private readonly IList order; + private readonly IDictionary m_pubRings; + private readonly IList m_order; - private PgpPublicKeyRingBundle( - IDictionary pubRings, - IList order) + private PgpPublicKeyRingBundle(IDictionary pubRings, IList 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 /// Input stream containing data. /// If a problem parsing the stream occurs. /// If an object is encountered which isn't a PgpPublicKeyRing. - public PgpPublicKeyRingBundle( - Stream inputStream) + public PgpPublicKeyRingBundle(Stream inputStream) : this(new PgpObjectFactory(inputStream).AllPgpObjects()) { } - public PgpPublicKeyRingBundle( - IEnumerable e) + public PgpPublicKeyRingBundle(IEnumerable e) { - this.pubRings = Platform.CreateHashtable(); - this.order = Platform.CreateArrayList(); + m_pubRings = new Dictionary(); + m_order = new List(); - 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); } } /// Return the number of key rings in this collection. public int Count { - get { return order.Count; } + get { return m_order.Count; } } /// Allow enumeration of the public key rings making up this collection. - public IEnumerable GetKeyRings() + public IEnumerable GetKeyRings() { - return new EnumerableProxy(pubRings.Values); + return CollectionUtilities.Proxy(m_pubRings.Values); } /// Allow enumeration of the key rings associated with the passed in userId. /// The user ID to be matched. /// An IEnumerable of key rings which matched (possibly none). - public IEnumerable GetKeyRings( - string userId) + public IEnumerable GetKeyRings(string userId) { return GetKeyRings(userId, false, false); } @@ -87,9 +80,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// The user ID to be matched. /// If true, userId need only be a substring of an actual ID string to match. /// An IEnumerable of key rings which matched (possibly none). - public IEnumerable GetKeyRings( - string userId, - bool matchPartial) + public IEnumerable GetKeyRings(string userId, bool matchPartial) { return GetKeyRings(userId, matchPartial, false); } @@ -99,12 +90,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// If true, userId need only be a substring of an actual ID string to match. /// If true, case is ignored in user ID comparisons. /// An IEnumerable of key rings which matched (possibly none). - public IEnumerable GetKeyRings( - string userId, - bool matchPartial, - bool ignoreCase) + public IEnumerable GetKeyRings(string userId, bool matchPartial, bool ignoreCase) { - IList rings = Platform.CreateArrayList(); + var rings = new List(); if (ignoreCase) { @@ -138,22 +126,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - return new EnumerableProxy(rings); + return CollectionUtilities.Proxy(rings); } /// Return the PGP public key associated with the given key id. /// The ID of the public key to return. - 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 /// Return the public key ring which contains the key referred to by keyId /// key ID to match against - 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. /// /// key ID to look for. - 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 /// The key ring to be added. /// A new PgpPublicKeyRingBundle merging the current one with the passed in key ring. /// If the keyId for the passed in key ring is already present. - 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(bundle.m_pubRings); + var newOrder = new List(bundle.m_order); newPubRings[key] = publicKeyRing; - newOrder.Add(key); return new PgpPublicKeyRingBundle(newPubRings, newOrder); @@ -251,19 +218,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// The key ring to be removed. /// A new PgpPublicKeyRingBundle not containing the passed in key ring. /// If the keyId for the passed in key ring is not present. - 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(bundle.m_pubRings); + var newOrder = new List(bundle.m_order); newPubRings.Remove(key); newOrder.Remove(key); -- cgit 1.4.1