/// <summary> /// Returns a new key ring with the public key passed in either added or /// replacing an existing one. /// </summary> /// <param name="pubRing">The public key ring to be modified.</param> /// <param name="pubKey">The public key to be inserted.</param> /// <returns>A new <c>PgpPublicKeyRing</c></returns> public static PgpPublicKeyRing InsertPublicKey(IPgpPublicKeyRing pubRing, IPgpPublicKey pubKey) { var keys = Platform.CreateArrayList(pubRing.GetPublicKeys()); var found = false; var masterFound = false; for (var i = 0; i != keys.Count; i++) { var key = keys[i]; if (key.KeyId == pubKey.KeyId) { found = true; keys[i] = pubKey; } if (key.IsMasterKey) { masterFound = true; } } if (!found) { if (pubKey.IsMasterKey) { if (masterFound) { throw new ArgumentException("cannot add a master key to a ring that already has one"); } keys.Insert(0, pubKey); } else { keys.Add(pubKey); } } return(new PgpPublicKeyRing(keys)); }
/// <summary> /// Returns a new key ring with the public key passed in either added or /// replacing an existing one. /// </summary> /// <param name="pubRing">The public key ring to be modified.</param> /// <param name="pubKey">The public key to be inserted.</param> /// <returns>A new <c>PgpPublicKeyRing</c></returns> public static PgpPublicKeyRing InsertPublicKey(IPgpPublicKeyRing pubRing, IPgpPublicKey pubKey) { var keys = Platform.CreateArrayList(pubRing.GetPublicKeys()); var found = false; var masterFound = false; for (var i = 0; i != keys.Count; i++) { var key = keys[i]; if (key.KeyId == pubKey.KeyId) { found = true; keys[i] = pubKey; } if (key.IsMasterKey) { masterFound = true; } } if (!found) { if (pubKey.IsMasterKey) { if (masterFound) throw new ArgumentException("cannot add a master key to a ring that already has one"); keys.Insert(0, pubKey); } else { keys.Add(pubKey); } } return new PgpPublicKeyRing(keys); }