Пример #1
0
        /*
         * Iterates all public keys in all keyrings in GnuPrivacyContext, and invokes given delegate for every key matching filter condition.
         */
        internal static void MatchingPublicKeys(ApplicationContext context, Node args, MatchingPublicKeysDelegate functor)
        {
            // House cleaning.
            using (new ArgsRemover(args, true)) {
                // Storing all filter given by caller in list, to avoid destroying enumeration iterator, due to modifying collection in caller's callback.
                var filters = XUtil.Iterate <string> (context, args).Where(ix => ix != null).ToList();

                // Creating new GnuPG context.
                using (var ctx = new GnuPrivacyContext()) {
                    // Iterating all secret keyrings.
                    foreach (PgpPublicKeyRing idxRing in ctx.PublicKeyRingBundle.GetKeyRings())
                    {
                        // Iterating all keys in currently iterated secret keyring.
                        foreach (PgpPublicKey idxPublicKey in idxRing.GetPublicKeys())
                        {
                            // Checking if caller provided filters, and if not, yielding "everything".
                            if (filters.Count == 0)
                            {
                                // No filters provided, matching everything.
                                functor(idxPublicKey);
                            }
                            else
                            {
                                // Iterating all filters given by caller.
                                foreach (var idxFilter in filters)
                                {
                                    // Checking if current filter is a match.
                                    if (IsMatch(idxPublicKey, idxFilter))
                                    {
                                        // Invoking callback supplied by caller, and breaking current filter enumeration, to avoid adding key twice.
                                        functor(idxPublicKey);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        /*
         * Iterates all public keys in all keyrings in GnuPrivacyContext, and invokes given delegate for every key matching filter condition.
         */
        internal static void MatchingPublicKeys(ApplicationContext context, Node args, MatchingPublicKeysDelegate functor, bool write)
        {
            // House cleaning.
            using (new ArgsRemover(args, true)) {
                // Storing all filter given by caller in list, to avoid destroying enumeration iterator, due to modifying collection in caller's callback.
                var filters = XUtil.Iterate <string> (context, args).Where(ix => ix != null).ToList();

                // Creating new GnuPG context.
                using (var ctx = new GnuPrivacyContext(write)) {
                    // Iterating all secret keyrings.
                    foreach (PgpPublicKeyRing idxRing in ctx.PublicKeyRingBundle.GetKeyRings())
                    {
                        // Iterating all keys in currently iterated secret keyring.
                        foreach (PgpPublicKey idxPublicKey in idxRing.GetPublicKeys())
                        {
                            // Verifying that this is a normal plain public key.
                            // Notice, we only return keys with at least one User ID.
                            if (!idxPublicKey.GetUserIds().GetEnumerator().MoveNext())
                            {
                                continue; // Probably just a signature for another key, or something.
                            }
                            // Checking if caller provided filters, and if not, yielding "everything".
                            if (filters.Count == 0)
                            {
                                // No filters provided, matching everything.
                                functor(idxPublicKey);
                            }
                            else
                            {
                                // Iterating all filters given by caller.
                                foreach (var idxFilter in filters)
                                {
                                    // Checking if current filter is a match.
                                    if (IsMatch(idxPublicKey, idxFilter))
                                    {
                                        // Invoking callback supplied by caller, and breaking current filter enumeration, to avoid adding key twice.
                                        functor(idxPublicKey);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }