Beispiel #1
0
        private static byte[] GetByteRangeDigest(PdfDocument document, PdfPKCS7 pkcs7, PdfSignature signature, string digestAlg)
        {
            Org.BouncyCastle.Crypto.IDigest         digest = Org.BouncyCastle.Security.DigestUtilities.GetDigest(digestAlg);
            iText.Kernel.Pdf.PdfArray               b      = signature.GetByteRange();
            iText.IO.Source.RandomAccessFileOrArray rf     = document.GetReader().GetSafeFile();
            Stream rg = null;

            try
            {
                rg = new iText.IO.Source.RASInputStream(new iText.IO.Source.RandomAccessSourceFactory().CreateRanged(rf.CreateSourceView(), b.ToLongArray(
                                                                                                                         )));
                byte[] buf = new byte[8192];
                int    rd;
                while ((rd = rg.Read(buf, 0, buf.Length)) > 0)
                {
                    digest.BlockUpdate(buf, 0, rd);
                }

                byte[] dig = new byte[digest.GetDigestSize()];
                digest.DoFinal(dig, 0);
                return(dig);
            }
            catch (Exception e)
            {
                throw new iText.Kernel.PdfException(e);
            }
            finally
            {
                try
                {
                    if (rg != null)
                    {
                        rg.Dispose();
                    }
                }
                catch (System.IO.IOException e)
                {
                    // this really shouldn't ever happen - the source view we use is based on a Safe view, which is a no-op anyway
                    throw new iText.Kernel.PdfException(e);
                }
            }
        }