PgpEncryptedData.cs :  » PDF » iTextSharp » Org » BouncyCastle » Bcpg » OpenPgp » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Bcpg » OpenPgp » PgpEncryptedData.cs
using System;
using System.Diagnostics;
using System.IO;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.IO;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Bcpg.OpenPgp{
    public abstract class PgpEncryptedData
    {
    internal class TruncatedStream
      : BaseInputStream
    {
      private const int LookAheadSize = 22;
      private const int LookAheadBufSize = 512;
      private const int LookAheadBufLimit = LookAheadBufSize - LookAheadSize;

      private readonly Stream inStr;
      private readonly byte[] lookAhead = new byte[LookAheadBufSize];
      private int bufStart, bufEnd;

      internal TruncatedStream(
        Stream inStr)
      {
        int numRead = Streams.ReadFully(inStr, lookAhead, 0, lookAhead.Length);

        if (numRead < LookAheadSize)
          throw new EndOfStreamException();

        this.inStr = inStr;
        this.bufStart = 0;
        this.bufEnd = numRead - LookAheadSize;
      }

      private int FillBuffer()
      {
        if (bufEnd < LookAheadBufLimit)
          return 0;

        Debug.Assert(bufStart == LookAheadBufLimit);
        Debug.Assert(bufEnd == LookAheadBufLimit);

        Array.Copy(lookAhead, LookAheadBufLimit, lookAhead, 0, LookAheadSize);
        bufEnd = Streams.ReadFully(inStr, lookAhead, LookAheadSize, LookAheadBufLimit);
        bufStart = 0;
        return bufEnd;
      }

      public override int ReadByte()
      {
        if (bufStart < bufEnd)
          return lookAhead[bufStart++];

        if (FillBuffer() < 1)
          return -1;

        return lookAhead[bufStart++];
      }

      public override int Read(byte[] buf, int off, int len)
      {
        int avail = bufEnd - bufStart;

        int pos = off;
        while (len > avail)
        {
          Array.Copy(lookAhead, bufStart, buf, pos, avail);

          bufStart += avail;
          pos += avail;
          len -= avail;

          if ((avail = FillBuffer()) < 1)
            return pos - off;
        }

        Array.Copy(lookAhead, bufStart, buf, pos, len);
        bufStart += len;

        return pos + len - off;;
      }

      internal byte[] GetLookAhead()
      {
        byte[] temp = new byte[LookAheadSize];
        Array.Copy(lookAhead, bufStart, temp, 0, LookAheadSize);
        return temp;
      }
    }

    internal InputStreamPacket  encData;
        internal Stream        encStream;
        internal TruncatedStream  truncStream;

    internal PgpEncryptedData(
            InputStreamPacket encData)
        {
            this.encData = encData;
        }

    /// <summary>Return the raw input stream for the data stream.</summary>
        public virtual Stream GetInputStream()
        {
            return encData.GetInputStream();
        }

    /// <summary>Return true if the message is integrity protected.</summary>
    /// <returns>True, if there is a modification detection code namespace associated
    /// with this stream.</returns>
        public bool IsIntegrityProtected()
        {
      return encData is SymmetricEncIntegrityPacket;
        }

    /// <summary>Note: This can only be called after the message has been read.</summary>
    /// <returns>True, if the message verifies, false otherwise</returns>
        public bool Verify()
        {
            if (!IsIntegrityProtected())
                throw new PgpException("data not integrity protected.");

      DigestStream dIn = (DigestStream) encStream;

      //
            // make sure we are at the end.
            //
            while (encStream.ReadByte() >= 0)
            {
        // do nothing
            }

      //
            // process the MDC packet
            //
      byte[] lookAhead = truncStream.GetLookAhead();

      IDigest hash = dIn.ReadDigest();
      hash.BlockUpdate(lookAhead, 0, 2);
      byte[] digest = DigestUtilities.DoFinal(hash);

      byte[] streamDigest = new byte[digest.Length];
      Array.Copy(lookAhead, 2, streamDigest, 0, streamDigest.Length);

      return Arrays.ConstantTimeAreEqual(digest, streamDigest);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.