<PackageReference Include="BouncyCastle.Cryptography" Version="2.7.0-beta.98" />

PgpSecretKeyRingBundle

public class PgpSecretKeyRingBundle
using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using System; using System.Collections.Generic; using System.IO; using System.Runtime.CompilerServices; namespace Org.BouncyCastle.Bcpg.OpenPgp { public class PgpSecretKeyRingBundle { private readonly IDictionary<long, PgpSecretKeyRing> m_secretRings; private readonly IList<long> m_order; public int Count => m_order.Count; private ICollection<PgpSecretKeyRing> KeyRings => m_secretRings.Values; private PgpSecretKeyRingBundle(IDictionary<long, PgpSecretKeyRing> secretRings, IList<long> order) { m_secretRings = secretRings; m_order = order; } public PgpSecretKeyRingBundle(byte[] encoding) : this(new MemoryStream(encoding, false)) { } public PgpSecretKeyRingBundle(Stream inputStream) : this(new PgpObjectFactory(inputStream).AllPgpObjects()) { } public PgpSecretKeyRingBundle(IEnumerable<PgpObject> e) { m_secretRings = new Dictionary<long, PgpSecretKeyRing>(); m_order = new List<long>(); foreach (PgpObject item in e) { if (!(item is PgpMarker)) { PgpSecretKeyRing pgpSecretKeyRing = item as PgpSecretKeyRing; if (pgpSecretKeyRing == null) throw new PgpException(Platform.GetTypeName(item) + " found where PgpSecretKeyRing expected"); long keyId = pgpSecretKeyRing.GetPublicKey().KeyId; m_secretRings.Add(keyId, pgpSecretKeyRing); m_order.Add(keyId); } } } public IEnumerable<PgpSecretKeyRing> GetKeyRings() { return CollectionUtilities.Proxy(KeyRings); } public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userId) { return GetKeyRings(userId, false, false); } public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userId, bool matchPartial) { return GetKeyRings(userId, matchPartial, false); } [IteratorStateMachine(typeof(<GetKeyRings>d__11))] public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userID, bool matchPartial, bool ignoreCase) { <GetKeyRings>d__11 <GetKeyRings>d__ = new <GetKeyRings>d__11(-2); <GetKeyRings>d__.<>4__this = this; <GetKeyRings>d__.<>3__userID = userID; <GetKeyRings>d__.<>3__matchPartial = matchPartial; <GetKeyRings>d__.<>3__ignoreCase = ignoreCase; return <GetKeyRings>d__; } public PgpSecretKey GetSecretKey(long keyId) { foreach (PgpSecretKeyRing keyRing in KeyRings) { PgpSecretKey secretKey = keyRing.GetSecretKey(keyId); if (secretKey != null) return secretKey; } return null; } public PgpSecretKeyRing GetSecretKeyRing(long keyId) { if (m_secretRings.TryGetValue(keyId, out PgpSecretKeyRing value)) return value; foreach (PgpSecretKeyRing keyRing in KeyRings) { if (keyRing.GetSecretKey(keyId) != null) return keyRing; } return null; } public bool Contains(long keyID) { return GetSecretKey(keyID) != null; } public byte[] GetEncoded() { MemoryStream memoryStream = new MemoryStream(); Encode(memoryStream); return memoryStream.ToArray(); } public void Encode(Stream outStr) { BcpgOutputStream outStr2 = BcpgOutputStream.Wrap(outStr); foreach (long item in m_order) { m_secretRings[item].Encode(outStr2); } } public static PgpSecretKeyRingBundle AddSecretKeyRing(PgpSecretKeyRingBundle bundle, PgpSecretKeyRing secretKeyRing) { long keyId = secretKeyRing.GetPublicKey().KeyId; if (bundle.m_secretRings.ContainsKey(keyId)) throw new ArgumentException("Collection already contains a key with a keyId for the passed in ring."); Dictionary<long, PgpSecretKeyRing> dictionary = new Dictionary<long, PgpSecretKeyRing>(bundle.m_secretRings); List<long> list = new List<long>(bundle.m_order); dictionary[keyId] = secretKeyRing; list.Add(keyId); return new PgpSecretKeyRingBundle(dictionary, list); } public static PgpSecretKeyRingBundle RemoveSecretKeyRing(PgpSecretKeyRingBundle bundle, PgpSecretKeyRing secretKeyRing) { long keyId = secretKeyRing.GetPublicKey().KeyId; if (!bundle.m_secretRings.ContainsKey(keyId)) throw new ArgumentException("Collection does not contain a key with a keyId for the passed in ring."); Dictionary<long, PgpSecretKeyRing> dictionary = new Dictionary<long, PgpSecretKeyRing>(bundle.m_secretRings); List<long> list = new List<long>(bundle.m_order); dictionary.Remove(keyId); list.Remove(keyId); return new PgpSecretKeyRingBundle(dictionary, list); } } }