Пример #1
0
        public void Padding_TryGetThicknessTest()
        {
            PaddingStyle target = new PaddingStyle();

            PDFThickness actual;
            PDFThickness expected = PDFThickness.Empty();
            bool         result   = target.TryGetThickness(out actual);

            Assert.IsFalse(result);
            Assert.AreEqual(expected, actual);

            target.All = 12;
            expected   = new PDFThickness(12);

            result = target.TryGetThickness(out actual);
            Assert.IsTrue(result);
            Assert.AreEqual(expected, actual);

            target.Left   = 13;
            target.Right  = 14;
            target.Top    = 15;
            target.Bottom = 16;
            expected      = new PDFThickness(15, 14, 16, 13);

            result = target.TryGetThickness(out actual);
            Assert.IsTrue(result);
            Assert.AreEqual(expected, actual);

            target.RemoveAllValues();
            expected = PDFThickness.Empty();
            result   = target.TryGetThickness(out actual);
            Assert.IsFalse(result);
            Assert.AreEqual(expected, actual);
        }
Пример #2
0
        public void Padding_AllTest()
        {
            PaddingStyle target = new PaddingStyle();

            Assert.AreEqual(PDFUnit.Zero, target.All);

            target.All = 20;
            Assert.AreEqual((PDFUnit)20, target.All);

            //These properties fall back to all
            Assert.AreEqual((PDFUnit)20, target.Left);
            Assert.AreEqual((PDFUnit)20, target.Top);
            Assert.AreEqual((PDFUnit)20, target.Bottom);
            Assert.AreEqual((PDFUnit)20, target.Right);

            //Modify one and make sure the rest as still set
            target.Bottom = 300;

            Assert.AreEqual((PDFUnit)20, target.All);
            Assert.AreEqual((PDFUnit)20, target.Left);
            Assert.AreEqual((PDFUnit)20, target.Top);
            Assert.AreEqual((PDFUnit)300, target.Bottom); //different
            Assert.AreEqual((PDFUnit)20, target.Right);

            target.RemoveAll();
            Assert.AreEqual(PDFUnit.Zero, target.All);

            Assert.AreEqual(PDFUnit.Zero, target.Left);
            Assert.AreEqual(PDFUnit.Zero, target.Top);
            Assert.AreEqual((PDFUnit)300, target.Bottom); //retained the separate value
            Assert.AreEqual(PDFUnit.Zero, target.Right);
        }
Пример #3
0
        public void PDFPaddingStyleConstructorTest()
        {
            PaddingStyle target = new PaddingStyle();

            Assert.IsNotNull(target);
            Assert.AreEqual(StyleKeys.PaddingItemKey, target.ItemKey);
        }
Пример #4
0
        public void Padding_TopTest()
        {
            PaddingStyle target = new PaddingStyle();

            Assert.AreEqual(PDFUnit.Zero, target.Top);

            target.Top = 20;
            Assert.AreEqual((PDFUnit)20, target.Top);

            target.RemoveTop();
            Assert.AreEqual(PDFUnit.Zero, target.Top);
        }
Пример #5
0
        public void Padding_RightTest()
        {
            PaddingStyle target = new PaddingStyle();

            Assert.AreEqual(PDFUnit.Zero, target.Right);

            target.Right = 20;
            Assert.AreEqual((PDFUnit)20, target.Right);

            target.RemoveRight();
            Assert.AreEqual(PDFUnit.Zero, target.Right);
        }
Пример #6
0
        public void Padding_BottomTest()
        {
            PaddingStyle target = new PaddingStyle();

            Assert.AreEqual(PDFUnit.Zero, target.Bottom);

            target.Bottom = 20;
            Assert.AreEqual((PDFUnit)20, target.Bottom);

            target.RemoveBottom();
            Assert.AreEqual(PDFUnit.Zero, target.Bottom);
        }
Пример #7
0
        public void Padding_SetThicknessTest()
        {
            PaddingStyle target = new PaddingStyle();

            target.All    = 12;
            target.Left   = 13;
            target.Right  = 14;
            target.Top    = 15;
            target.Bottom = 16;

            PDFThickness thickness = new PDFThickness(21, 22, 23, 24); //T,R,B,L

            target.SetThickness(thickness);

            Assert.AreEqual((PDFUnit)21, target.Top);
            Assert.AreEqual((PDFUnit)24, target.Left);
            Assert.AreEqual((PDFUnit)23, target.Bottom);
            Assert.AreEqual((PDFUnit)22, target.Right);
        }
Пример #8
0
        public static byte[] Unpad(byte[] data, int blockSize, PaddingStyle paddingStyle)
        {
            switch (paddingStyle)
            {
            case PaddingStyle.None:
                return(data);

            case PaddingStyle.Pkcs7:
                return(UnpadPkcs7(data, blockSize));

            case PaddingStyle.AnsiX923:
                return(UnpadAnsiX923(data, blockSize));

            case PaddingStyle.Iso7816:
                return(UnpadIso7816(data, blockSize));

            case PaddingStyle.Iso10126:
                return(UnpadIso10126(data, blockSize));

            default:
                throw new InvalidOperationException("Unknown padding style");
            }
        }
Пример #9
0
        /// <summary>
        /// Encrypt stream with Blowfish-CBC
        /// </summary>
        /// <param name="input">Input stream to encrypt</param>
        /// <param name="output">Output stream</param>
        /// <param name="key">Key</param>
        /// <param name="iv">IV</param>
        /// <param name="paddingStyle">Padding</param>
        /// <param name="notifyProgression">Notify progression method</param>
        /// <param name="bufferSize">Buffer size</param>
        public static void EncryptCBC(Stream input, Stream output, byte[] key, byte[] iv, PaddingStyle paddingStyle = PaddingStyle.Pkcs7, Action <int> notifyProgression = null, int bufferSize = 4096)
        {
            IBufferedCipher  cipher     = new BufferedBlockCipher(new CbcBlockCipher(new BlowfishEngine()));
            ParametersWithIV parameters = new ParametersWithIV(new KeyParameter(key, 0, key.Length), iv, 0, iv.Length);

            cipher.Init(true, parameters);

            bool padDone = false;
            int  bytesRead;

            byte[] buffer = new byte[bufferSize];
            byte[] enc    = new byte[bufferSize];

            do
            {
                bytesRead = input.Read(buffer, 0, bufferSize);

                if (bytesRead == bufferSize)
                {
                    cipher.ProcessBytes(buffer, enc, 0);
                    output.Write(enc, 0, bytesRead);
                }
                else if (bytesRead > 0)
                {
                    byte[] smallBuffer = new byte[bytesRead];
                    Array.Copy(buffer, 0, smallBuffer, 0, bytesRead);
                    byte[] padData = Padding.Pad(smallBuffer, BLOCK_SIZE, paddingStyle);
                    cipher.ProcessBytes(padData, enc, 0);
                    output.Write(enc, 0, padData.Length);
                    padDone = true;
                }

                if (notifyProgression != null && bytesRead > 0)
                {
                    notifyProgression(bytesRead);
                }
            } while (bytesRead == bufferSize);

            if (!padDone)
            {
                buffer = new byte[0];
                byte[] padData = Padding.Pad(buffer, BLOCK_SIZE, paddingStyle);
                cipher.ProcessBytes(padData, enc, 0);
                output.Write(enc, 0, padData.Length);
            }
        }
Пример #10
0
        /// <summary>
        /// Decrypt stream with Blowfish-CBC
        /// </summary>
        /// <param name="input">Input stream to decrypt</param>
        /// <param name="output">Output stream</param>
        /// <param name="key">Key</param>
        /// <param name="iv">IV</param>
        /// <param name="paddingStyle">Padding</param>
        /// <param name="notifyProgression">Notify progression method</param>
        /// <param name="bufferSize">Buffer size</param>
        public static void DecryptCBC(Stream input, Stream output, byte[] key, byte[] iv, PaddingStyle paddingStyle = PaddingStyle.Pkcs7, Action <int> notifyProgression = null, int bufferSize = 4096)
        {
            IBufferedCipher  cipher     = new BufferedBlockCipher(new CbcBlockCipher(new BlowfishEngine()));
            ParametersWithIV parameters = new ParametersWithIV(new KeyParameter(key, 0, key.Length), iv, 0, iv.Length);

            cipher.Init(false, parameters);

            byte[] backup = null;
            int    bytesRead;

            byte[] buffer = new byte[bufferSize];
            byte[] dec    = new byte[bufferSize];

            do
            {
                bytesRead = input.Read(buffer, 0, bufferSize);

                if (bytesRead > 0)
                {
                    if (backup != null)
                    {
                        output.Write(backup, 0, backup.Length);
                        backup = null;
                    }

                    if (bytesRead == bufferSize)
                    {
                        cipher.ProcessBytes(buffer, dec, 0);
                        backup = new byte[bytesRead];
                        Array.Copy(dec, 0, backup, 0, bytesRead);
                    }
                    else
                    {
                        dec = new byte[bytesRead];
                        byte[] smallBuffer = new byte[bytesRead];
                        Array.Copy(buffer, 0, smallBuffer, 0, bytesRead);
                        cipher.ProcessBytes(smallBuffer, dec, 0);
                        byte[] unpadData = Padding.Unpad(dec, BLOCK_SIZE, paddingStyle);
                        output.Write(unpadData, 0, unpadData.Length);
                    }

                    if (notifyProgression != null)
                    {
                        notifyProgression(bytesRead);
                    }
                }
                else
                {
                    if (backup != null)
                    {
                        byte[] unpadData = Padding.Unpad(backup, BLOCK_SIZE, paddingStyle);
                        output.Write(unpadData, 0, unpadData.Length);
                    }
                }
            } while (bytesRead == bufferSize);
        }