Ejemplo n.º 1
0
 /// <summary>
 /// Returns actual secret key, in armored ASCII format.
 /// </summary>
 /// <param name="key">Key to retrieve.</param>
 /// <returns>Armored ASCII format representing private key.</returns>
 public static string GetAsciiArmoredSecretKey(PgpSecretKey key)
 {
     using (var memStream = new MemoryStream())
     {
         using (var armored = new ArmoredOutputStream(memStream))
         {
             key.Encode(armored);
             armored.Flush();
         }
         memStream.Flush();
         memStream.Position = 0;
         using (var sr = new StreamReader(memStream))
         {
             return(sr.ReadToEnd());
         }
     }
 }
Ejemplo n.º 2
0
        static void p5_crypto_get_private_key(ApplicationContext context, ActiveEventArgs e)
        {
            // Using common helper to iterate all secret keys.
            ObjectIterator.MatchingPrivateKeys(context, e.Args, delegate(PgpSecretKey key) {
                // Retrieving fingerprint of currently iterated key, and returning to caller.
                var fingerprint = BitConverter.ToString(key.PublicKey.GetFingerprint()).Replace("-", "").ToLower();
                var node        = e.Args.Add(fingerprint).LastChild;

                // This is the key we're looking for
                using (var memStream = new MemoryStream()) {
                    using (var armored = new ArmoredOutputStream(memStream)) {
                        key.Encode(armored);
                        armored.Flush();
                    }
                    memStream.Flush();
                    memStream.Position = 0;
                    var sr             = new StreamReader(memStream);
                    node.Value         = sr.ReadToEnd();
                }
            }, false);
        }
Ejemplo n.º 3
0
        private static void p5_crypto_get_public_key(ApplicationContext context, ActiveEventArgs e)
        {
            // House cleaning
            using (new ArgsRemover(e.Args)) {
                // Getting key ID to look for
                string keyID = e.Args.GetExValue <string> (context, null);
                if (string.IsNullOrEmpty(keyID))
                {
                    throw new LambdaException("No ID given to use for looking up key", e.Args, context);
                }

                // Creating new GnuPG context
                using (var ctx = new GnuPrivacyContext()) {
                    // Looping through each public key in GnuPG database
                    foreach (PgpPublicKeyRing idxRing in ctx.PublicKeyRingBundle.GetKeyRings())
                    {
                        // Looping through each key in keyring
                        foreach (PgpPublicKey idxPublicKey in idxRing.GetPublicKeys())
                        {
                            // Checking if this is the requested key
                            if (idxPublicKey.KeyId.ToString("X") == keyID)
                            {
                                // This is the key we're looking for
                                using (var memStream = new MemoryStream()) {
                                    using (var armored = new ArmoredOutputStream(memStream)) {
                                        idxPublicKey.Encode(armored);
                                        armored.Flush();
                                    }
                                    memStream.Flush();
                                    memStream.Position = 0;
                                    var sr = new StreamReader(memStream);
                                    e.Args.Value = sr.ReadToEnd();
                                }
                                return;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        static void p5_crypto_pgp_keys_public_get(ApplicationContext context, ActiveEventArgs e)
        {
            // Using common helper to iterate all public keyrings matching filter.
            PGPKeyIterator.Find(context, e.Args, delegate(OpenPgpContext ctx, PgpPublicKeyRing keyring) {
                // Retrieving fingerprint of currently iterated key, and returning to caller.
                var key         = keyring.GetPublicKey();
                var fingerprint = Fingerprint.FingerprintString(key.GetFingerprint());
                var node        = e.Args.Add(fingerprint).LastChild;

                // Returning public key as armored ASCII.
                using (var memStream = new MemoryStream()) {
                    using (var armored = new ArmoredOutputStream(memStream)) {
                        key.Encode(armored);
                        armored.Flush();
                    }
                    memStream.Flush();
                    memStream.Position = 0;
                    var sr             = new StreamReader(memStream);
                    node.Value         = sr.ReadToEnd();
                }
            }, false);
        }