diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-28 15:37:25 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-28 15:37:25 +0700 |
commit | 3ce45e18811232677715a3bf4ba144a72b639942 (patch) | |
tree | 228c87fb260e125c66b34334cedb9b34b07a4dac /crypto/src/openpgp/PgpSecretKeyRingBundle.cs | |
parent | Generics migration in Crmf, Crypto, Math (diff) | |
download | BouncyCastle.NET-ed25519-3ce45e18811232677715a3bf4ba144a72b639942.tar.xz |
Generics migration in Ocsp, OpenPgp
Diffstat (limited to 'crypto/src/openpgp/PgpSecretKeyRingBundle.cs')
-rw-r--r-- | crypto/src/openpgp/PgpSecretKeyRingBundle.cs | 121 |
1 files changed, 42 insertions, 79 deletions
diff --git a/crypto/src/openpgp/PgpSecretKeyRingBundle.cs b/crypto/src/openpgp/PgpSecretKeyRingBundle.cs index f367d17d0..994f4f718 100644 --- a/crypto/src/openpgp/PgpSecretKeyRingBundle.cs +++ b/crypto/src/openpgp/PgpSecretKeyRingBundle.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 PgpSecretKeyRingBundle { - private readonly IDictionary secretRings; - private readonly IList order; + private readonly IDictionary<long, PgpSecretKeyRing> m_secretRings; + private readonly IList<long> m_order; - private PgpSecretKeyRingBundle( - IDictionary secretRings, - IList order) + private PgpSecretKeyRingBundle(IDictionary<long, PgpSecretKeyRing> secretRings, IList<long> order) { - this.secretRings = secretRings; - this.order = order; + m_secretRings = secretRings; + m_order = order; } - public PgpSecretKeyRingBundle( - byte[] encoding) + public PgpSecretKeyRingBundle(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 PgpSecretKeyRing.</exception> - public PgpSecretKeyRingBundle( - Stream inputStream) + public PgpSecretKeyRingBundle(Stream inputStream) : this(new PgpObjectFactory(inputStream).AllPgpObjects()) { } - public PgpSecretKeyRingBundle( - IEnumerable e) + public PgpSecretKeyRingBundle(IEnumerable<PgpObject> e) { - this.secretRings = Platform.CreateHashtable(); - this.order = Platform.CreateArrayList(); + m_secretRings = new Dictionary<long, PgpSecretKeyRing>(); + 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; - PgpSecretKeyRing pgpSecret = obj as PgpSecretKeyRing; - if (pgpSecret == null) + if (!(obj is PgpSecretKeyRing pgpSecret)) throw new PgpException(Platform.GetTypeName(obj) + " found where PgpSecretKeyRing expected"); long key = pgpSecret.GetPublicKey().KeyId; - secretRings.Add(key, pgpSecret); - order.Add(key); + m_secretRings.Add(key, pgpSecret); + m_order.Add(key); } } /// <summary>Return the number of rings in this collection.</summary> public int Count { - get { return order.Count; } + get { return m_order.Count; } } /// <summary>Allow enumeration of the secret key rings making up this collection.</summary> - public IEnumerable GetKeyRings() + public IEnumerable<PgpSecretKeyRing> GetKeyRings() { - return new EnumerableProxy(secretRings.Values); + return CollectionUtilities.Proxy(m_secretRings.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<PgpSecretKeyRing> 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<PgpSecretKeyRing> 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<PgpSecretKeyRing> GetKeyRings(string userId, bool matchPartial, bool ignoreCase) { - IList rings = Platform.CreateArrayList(); + var rings = new List<PgpSecretKeyRing>(); if (ignoreCase) { @@ -138,22 +126,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - return new EnumerableProxy(rings); + return CollectionUtilities.Proxy(rings); } /// <summary>Return the PGP secret key associated with the given key id.</summary> /// <param name="keyId">The ID of the secret key to return.</param> - public PgpSecretKey GetSecretKey( - long keyId) + public PgpSecretKey GetSecretKey(long keyId) { foreach (PgpSecretKeyRing secRing in GetKeyRings()) { PgpSecretKey sec = secRing.GetSecretKey(keyId); - if (sec != null) - { return sec; - } } return null; @@ -161,24 +145,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Return the secret key ring which contains the key referred to by keyId</summary> /// <param name="keyId">The ID of the secret key</param> - public PgpSecretKeyRing GetSecretKeyRing( - long keyId) + public PgpSecretKeyRing GetSecretKeyRing(long keyId) { - long id = keyId; - - if (secretRings.Contains(id)) - { - return (PgpSecretKeyRing) secretRings[id]; - } + if (m_secretRings.TryGetValue(keyId, out var keyRing)) + return keyRing; foreach (PgpSecretKeyRing secretRing in GetKeyRings()) { - PgpSecretKey secret = secretRing.GetSecretKey(keyId); - - if (secret != null) - { + if (secretRing.GetSecretKey(keyId) != null) return secretRing; - } } return null; @@ -188,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 GetSecretKey(keyID) != null; } @@ -197,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) { - PgpSecretKeyRing pub = (PgpSecretKeyRing) secretRings[key]; - - pub.Encode(bcpgOut); + m_secretRings[key].Encode(bcpgOut); } } @@ -224,19 +193,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="secretKeyRing">The key ring to be added.</param> /// <returns>A new <c>PgpSecretKeyRingBundle</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 PgpSecretKeyRingBundle AddSecretKeyRing( - PgpSecretKeyRingBundle bundle, - PgpSecretKeyRing secretKeyRing) + public static PgpSecretKeyRingBundle AddSecretKeyRing(PgpSecretKeyRingBundle bundle, + PgpSecretKeyRing secretKeyRing) { long key = secretKeyRing.GetPublicKey().KeyId; - if (bundle.secretRings.Contains(key)) - { + if (bundle.m_secretRings.ContainsKey(key)) throw new ArgumentException("Collection already contains a key with a keyId for the passed in ring."); - } - IDictionary newSecretRings = Platform.CreateHashtable(bundle.secretRings); - IList newOrder = Platform.CreateArrayList(bundle.order); + var newSecretRings = new Dictionary<long, PgpSecretKeyRing>(bundle.m_secretRings); + var newOrder = new List<long>(bundle.m_order); newSecretRings[key] = secretKeyRing; newOrder.Add(key); @@ -252,19 +218,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="secretKeyRing">The key ring to be removed.</param> /// <returns>A new <c>PgpSecretKeyRingBundle</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 PgpSecretKeyRingBundle RemoveSecretKeyRing( - PgpSecretKeyRingBundle bundle, - PgpSecretKeyRing secretKeyRing) + public static PgpSecretKeyRingBundle RemoveSecretKeyRing(PgpSecretKeyRingBundle bundle, + PgpSecretKeyRing secretKeyRing) { long key = secretKeyRing.GetPublicKey().KeyId; - if (!bundle.secretRings.Contains(key)) - { + if (!bundle.m_secretRings.ContainsKey(key)) throw new ArgumentException("Collection does not contain a key with a keyId for the passed in ring."); - } - IDictionary newSecretRings = Platform.CreateHashtable(bundle.secretRings); - IList newOrder = Platform.CreateArrayList(bundle.order); + var newSecretRings = new Dictionary<long, PgpSecretKeyRing>(bundle.m_secretRings); + var newOrder = new List<long>(bundle.m_order); newSecretRings.Remove(key); newOrder.Remove(key); |