Beispiel #1
0
        public void Can_Encrypt_And_Decrypt_Text_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalText.txt");

            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    AlwaysTrustPublicKey = false,
                    InputData            = origFs,
                    Operation            = DataOperation.Encrypt,
                    Recipient            = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData  = encryptStream,
                        Operation  = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                        using (StreamReader reader = new StreamReader(decryptedStream))
                        {
                            string origText  = File.ReadAllText(origFile);
                            string finalText = reader.ReadToEnd();
                            Assert.AreEqual(origText, finalText, "Roundtrip got diffent text.");
                        }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Processes data with stream input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>
        /// Output stream.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">input</exception>
        /// <exception cref="PgpException"></exception>
        public Stream ProcessData(StreamDataInput input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            input.Verify();

            // only way to reliably make this work is save to file and process it instead.
            string tempInFile  = null;
            string tempOutFile = null;

            try
            {
                tempInFile  = Path.GetTempFileName();
                tempOutFile = Path.GetTempFileName();

                using (var fs = File.OpenWrite(tempInFile))
                {
                    input.InputData.CopyTo(fs);
                }
                var newArg = new FileDataInput
                {
                    Armor = input.Armor,
                    AlwaysTrustPublicKey = input.AlwaysTrustPublicKey,
                    InputFile            = tempInFile,
                    Operation            = input.Operation,
                    Originator           = input.Originator,
                    OutputFile           = tempOutFile,
                    Passphrase           = input.Passphrase,
                    Recipient            = input.Recipient
                };
                ProcessData(newArg);
                return(new TempFileStream(tempOutFile));
            }
            catch
            {
                IOUtility.DeleteFiles(tempOutFile);
                throw;
            }
            finally
            {
                IOUtility.DeleteFiles(tempInFile);
            }
        }
Beispiel #3
0
        public void Can_Encrypt_And_Decrypt_Binary_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalBinary.png");

            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    AlwaysTrustPublicKey = false,
                    InputData            = origFs,
                    Operation            = DataOperation.Encrypt,
                    Recipient            = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData  = encryptStream,
                        Operation  = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                        using (MemoryStream testStream = new MemoryStream())
                        {
                            decryptedStream.CopyTo(testStream);

                            byte[] origBytes  = File.ReadAllBytes(origFile);
                            byte[] finalBytes = testStream.ToArray();
                            CollectionAssert.AreEqual(origBytes, finalBytes, "Roundtrip got diffent bytes.");
                        }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Processes data with stream input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>
        /// Output stream.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">input</exception>
        /// <exception cref="PgpException"></exception>
        public Stream ProcessData(StreamDataInput input)
        {
            if (input == null) { throw new ArgumentNullException("input"); }
            input.Verify();

            // only way to reliably make this work is save to file and process it instead.
            string tempInFile = null;
            string tempOutFile = null;
            try
            {
                tempInFile = Path.GetTempFileName();
                tempOutFile = Path.GetTempFileName();

                using (var fs = File.OpenWrite(tempInFile))
                {
                    input.InputData.CopyTo(fs);
                }
                var newArg = new FileDataInput
                {
                    Armor = input.Armor,
                    AlwaysTrustPublicKey = input.AlwaysTrustPublicKey,
                    InputFile = tempInFile,
                    Operation = input.Operation,
                    Originator = input.Originator,
                    OutputFile = tempOutFile,
                    Passphrase = input.Passphrase,
                    Recipient = input.Recipient
                };
                ProcessData(newArg);
                return new TempFileStream(tempOutFile);
            }
            catch
            {
                IOUtility.DeleteFiles(tempOutFile);
                throw;
            }
            finally
            {
                IOUtility.DeleteFiles(tempInFile);
            }
        }
Beispiel #5
0
        public void Can_Encrypt_And_Decrypt_Binary_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalBinary.png");
            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    InputData = origFs,
                    Operation = DataOperation.Encrypt,
                    Recipient = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData = encryptStream,
                        Operation = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                    using (MemoryStream testStream = new MemoryStream())
                    {
                        decryptedStream.CopyTo(testStream);

                        byte[] origBytes = File.ReadAllBytes(origFile);
                        byte[] finalBytes = testStream.ToArray();
                        CollectionAssert.AreEqual(origBytes, finalBytes, "Roundtrip got diffent bytes.");
                    }
                }
            }
        }
Beispiel #6
0
        public void Can_Encrypt_And_Decrypt_Text_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalText.txt");
            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    InputData = origFs,
                    Operation = DataOperation.Encrypt,
                    Recipient = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData = encryptStream,
                        Operation = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                    using (StreamReader reader = new StreamReader(decryptedStream))
                    {
                        string origText = File.ReadAllText(origFile);
                        string finalText = reader.ReadToEnd();
                        Assert.AreEqual(origText, finalText, "Roundtrip got diffent text.");
                    }
                }
            }
        }