BufferedAeadBlockCipher.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » 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 » Crypto » BufferedAeadBlockCipher.cs
using System;

using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;

namespace Org.BouncyCastle.Crypto{
  /**
  * The AEAD block ciphers already handle buffering internally, so this class
  * just takes care of implementing IBufferedCipher methods.
  */
  public class BufferedAeadBlockCipher
    : BufferedCipherBase
  {
    private readonly IAeadBlockCipher cipher;

    public BufferedAeadBlockCipher(
      IAeadBlockCipher cipher)
    {
      if (cipher == null)
        throw new ArgumentNullException("cipher");

      this.cipher = cipher;
    }

    public override string AlgorithmName
    {
      get { return cipher.AlgorithmName; }
    }

    /**
    * initialise the cipher.
    *
    * @param forEncryption if true the cipher is initialised for
    *  encryption, if false for decryption.
    * @param param the key and other data required by the cipher.
    * @exception ArgumentException if the parameters argument is
    * inappropriate.
    */
    public override void Init(
      bool        forEncryption,
      ICipherParameters  parameters)
    {
      if (parameters is ParametersWithRandom)
      {
        parameters = ((ParametersWithRandom) parameters).Parameters;
      }

      cipher.Init(forEncryption, parameters);
    }

    /**
    * return the blocksize for the underlying cipher.
    *
    * @return the blocksize for the underlying cipher.
    */
    public override int GetBlockSize()
    {
      return cipher.GetBlockSize();
    }

    /**
    * return the size of the output buffer required for an update
    * an input of len bytes.
    *
    * @param len the length of the input.
    * @return the space required to accommodate a call to update
    * with len bytes of input.
    */
    public override int GetUpdateOutputSize(
      int length)
    {
      return cipher.GetUpdateOutputSize(length);
    }

    /**
    * return the size of the output buffer required for an update plus a
    * doFinal with an input of len bytes.
    *
    * @param len the length of the input.
    * @return the space required to accommodate a call to update and doFinal
    * with len bytes of input.
    */
    public override int GetOutputSize(
      int length)
    {
      return cipher.GetOutputSize(length);
    }

    /**
    * process a single byte, producing an output block if neccessary.
    *
    * @param in the input byte.
    * @param out the space for any output that might be produced.
    * @param outOff the offset from which the output will be copied.
    * @return the number of output bytes copied to out.
    * @exception DataLengthException if there isn't enough space in out.
    * @exception InvalidOperationException if the cipher isn't initialised.
    */
    public override int ProcessByte(
      byte  input,
      byte[]  output,
      int    outOff)
    {
      return cipher.ProcessByte(input, output, outOff);
    }

    public override byte[] ProcessByte(
      byte input)
    {
      int outLength = GetUpdateOutputSize(1);

      byte[] outBytes = outLength > 0 ? new byte[outLength] : null;

      int pos = ProcessByte(input, outBytes, 0);

      if (outLength > 0 && pos < outLength)
      {
        byte[] tmp = new byte[pos];
        Array.Copy(outBytes, 0, tmp, 0, pos);
        outBytes = tmp;
      }

      return outBytes;
    }

    public override byte[] ProcessBytes(
      byte[]  input,
      int    inOff,
      int    length)
    {
      if (input == null)
        throw new ArgumentNullException("input");
      if (length < 1)
        return null;

      int outLength = GetUpdateOutputSize(length);

      byte[] outBytes = outLength > 0 ? new byte[outLength] : null;

      int pos = ProcessBytes(input, inOff, length, outBytes, 0);

      if (outLength > 0 && pos < outLength)
      {
        byte[] tmp = new byte[pos];
        Array.Copy(outBytes, 0, tmp, 0, pos);
        outBytes = tmp;
      }

      return outBytes;
    }

    /**
    * process an array of bytes, producing output if necessary.
    *
    * @param in the input byte array.
    * @param inOff the offset at which the input data starts.
    * @param len the number of bytes to be copied out of the input array.
    * @param out the space for any output that might be produced.
    * @param outOff the offset from which the output will be copied.
    * @return the number of output bytes copied to out.
    * @exception DataLengthException if there isn't enough space in out.
    * @exception InvalidOperationException if the cipher isn't initialised.
    */
    public override int ProcessBytes(
      byte[]  input,
      int    inOff,
      int    length,
      byte[]  output,
      int    outOff)
    {
      return cipher.ProcessBytes(input, inOff, length, output, outOff);
    }

    public override byte[] DoFinal()
    {
      byte[] outBytes = EmptyBuffer;

      int length = GetOutputSize(0);
      if (length > 0)
      {
        outBytes = new byte[length];

        int pos = DoFinal(outBytes, 0);
        if (pos < outBytes.Length)
        {
          byte[] tmp = new byte[pos];
          Array.Copy(outBytes, 0, tmp, 0, pos);
          outBytes = tmp;
        }
      }

      return outBytes;
    }

    public override byte[] DoFinal(
      byte[]  input,
      int    inOff,
      int    inLen)
    {
      if (input == null)
        throw new ArgumentNullException("input");

      int length = GetOutputSize(inLen);

      byte[] outBytes = EmptyBuffer;

      if (length > 0)
      {
        outBytes = new byte[length];

        int pos = (inLen > 0)
          ?  ProcessBytes(input, inOff, inLen, outBytes, 0)
          :  0;

        pos += DoFinal(outBytes, pos);

        if (pos < outBytes.Length)
        {
          byte[] tmp = new byte[pos];
          Array.Copy(outBytes, 0, tmp, 0, pos);
          outBytes = tmp;
        }
      }

      return outBytes;
    }

    /**
    * Process the last block in the buffer.
    *
    * @param out the array the block currently being held is copied into.
    * @param outOff the offset at which the copying starts.
    * @return the number of output bytes copied to out.
    * @exception DataLengthException if there is insufficient space in out for
    * the output, or the input is not block size aligned and should be.
    * @exception InvalidOperationException if the underlying cipher is not
    * initialised.
    * @exception InvalidCipherTextException if padding is expected and not found.
    * @exception DataLengthException if the input is not block size
    * aligned.
    */
    public override int DoFinal(
      byte[]  output,
      int    outOff)
    {
      return cipher.DoFinal(output, outOff);
    }

    /**
    * Reset the buffer and cipher. After resetting the object is in the same
    * state as it was after the last init (if there was one).
    */
    public override void Reset()
    {
      cipher.Reset();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.