Beispiel #1
0
        /// <inheritdoc/>
        protected override byte[] HashFinal()
        {
            var output = new byte[digest.GetDigestSize()];

            digest.DoFinal(output, 0);
            return(output);
        }
Beispiel #2
0
 public static byte[] Digest(Org.BouncyCastle.Crypto.IDigest d, byte[] b, int offset, int len)
 {
     d.BlockUpdate(b, offset, len);
     byte[] r = new byte[d.GetDigestSize()];
     d.DoFinal(r, 0);
     return(r);
 }
Beispiel #3
0
        } // End Function GetBouncyAlgorithm

        protected override byte[] HashData(byte[] data, int offset, int count,
                                           System.Security.Cryptography.HashAlgorithmName hashAlgorithm)
        {
            Org.BouncyCastle.Crypto.IDigest digest = GetBouncyAlgorithm(hashAlgorithm);

            byte[] retValue = new byte[digest.GetDigestSize()];
            digest.BlockUpdate(data, offset, count);
            digest.DoFinal(retValue, 0);
            return(retValue);
        } // End Function HashData
Beispiel #4
0
        public static byte[] Digest(System.IO.Stream data, Org.BouncyCastle.Crypto.IDigest messageDigest)
        {
            byte[] buf = new byte[8192];
            int    n;

            while ((n = data.Read(buf, 0, buf.Length)) > 0)
            {
                messageDigest.BlockUpdate(buf, 0, n);
            }
            byte[] r = new byte[messageDigest.GetDigestSize()];
            messageDigest.DoFinal(r, 0);
            return(r);
        }
Beispiel #5
0
        } // End Function HashData

        protected override byte[] HashData(System.IO.Stream data,
                                           System.Security.Cryptography.HashAlgorithmName hashAlgorithm)
        {
            Org.BouncyCastle.Crypto.IDigest digest = GetBouncyAlgorithm(hashAlgorithm);

            byte[] buffer = new byte[4096];
            int    cbSize;

            while ((cbSize = data.Read(buffer, 0, buffer.Length)) > 0)
            {
                digest.BlockUpdate(buffer, 0, cbSize);
            }

            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);
            return(hash);
        } // End Function HashData
Beispiel #6
0
        public WindowsPrng()
        {
            // Don't use the bugged CryptoAPI
            // this.m_rnd = new Org.BouncyCastle.Crypto.Prng.CryptoApiRandomGenerator();

            Org.BouncyCastle.Crypto.IDigest digest = Org.BouncyCastle.Security.DigestUtilities.GetDigest("SHA256");
            if (digest == null)
            {
                return;
            }

            Org.BouncyCastle.Crypto.Prng.DigestRandomGenerator prng =
                new Org.BouncyCastle.Crypto.Prng.DigestRandomGenerator(digest);

            prng.AddSeedMaterial(NextCounterValue());
            prng.AddSeedMaterial(GetNextBytes(digest.GetDigestSize()));

            this.m_rnd = prng;
        }
Beispiel #7
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);
                }
            }
        }