/// <summary>
        /// Return a new bundle containing the contents of the passed in bundle with
        /// the passed in secret key ring removed.
        /// </summary>
        /// <param name="bundle">The <c>PgpSecretKeyRingBundle</c> the key ring is to be removed from.</param>
        /// <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)
        {
            long key = secretKeyRing.GetPublicKey().KeyId;

            if (!bundle.secretRings.ContainsKey(key))
            {
                throw new ArgumentException("Collection does not contain a key with a keyId for the passed in ring.");
            }

            IDictionary <long, PgpSecretKeyRing> newSecretRings = new Dictionary <long, PgpSecretKeyRing>(bundle.secretRings);
            IList <long> newOrder = new List <long>(bundle.order);

            newSecretRings.Remove(key);
            newOrder.Remove(key);

            return(new PgpSecretKeyRingBundle(newSecretRings, newOrder));
        }
Example #2
0
        /// <summary>
        /// Return a copy of the passed in secret key ring, with the master key and sub keys encrypted
        /// using a new password and the passed in algorithm.
        /// </summary>
        /// <param name="ring">The <c>PgpSecretKeyRing</c> to be copied.</param>
        /// <param name="oldPassPhrase">The current password for key.</param>
        /// <param name="newPassPhrase">The new password for the key.</param>
        /// <param name="newEncAlgorithm">The algorithm to be used for the encryption.</param>
        /// <param name="rand">Source of randomness.</param>
        public static PgpSecretKeyRing CopyWithNewPassword(
            PgpSecretKeyRing ring,
            ReadOnlySpan <char> oldPassPhrase,
            ReadOnlySpan <char> newPassPhrase)
        {
            IList <PgpSecretKey> newKeys = new List <PgpSecretKey>(ring.keys.Count);

            foreach (PgpSecretKey secretKey in ring.GetSecretKeys())
            {
                if (secretKey.IsPrivateKeyEmpty)
                {
                    newKeys.Add(secretKey);
                }
                else
                {
                    newKeys.Add(PgpSecretKey.CopyWithNewPassword(secretKey, oldPassPhrase, newPassPhrase));
                }
            }

            return(new PgpSecretKeyRing(newKeys, ring.extraPubKeys));
        }