NotationData.cs :  » PDF » iTextSharp » Org » BouncyCastle » Bcpg » Sig » 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 » Sig » NotationData.cs
using System;
using System.IO;
using System.Text;

namespace Org.BouncyCastle.Bcpg.Sig{
  /**
  * Class provided a NotationData object according to
  * RFC2440, Chapter 5.2.3.15. Notation Data
  */
  public class NotationData
    : SignatureSubpacket
  {
    public const int HeaderFlagLength = 4;
    public const int HeaderNameLength = 2;
    public const int HeaderValueLength = 2;

    public NotationData(
      bool  critical,
      byte[]  data)
      : base(SignatureSubpacketTag.NotationData, critical, data)
    {
    }

    public NotationData(
      bool  critical,
      bool  humanReadable,
      string  notationName,
      string  notationValue)
      : base(SignatureSubpacketTag.NotationData, critical,
        createData(humanReadable, notationName, notationValue))
    {
    }

    private static byte[] createData(
      bool  humanReadable,
      string  notationName,
      string  notationValue)
    {
      MemoryStream os = new MemoryStream();

      // (4 octets of flags, 2 octets of name length (M),
      // 2 octets of value length (N),
      // M octets of name data,
      // N octets of value data)

      // flags
      os.WriteByte(humanReadable ? (byte)0x80 : (byte)0x00);
      os.WriteByte(0x0);
      os.WriteByte(0x0);
      os.WriteByte(0x0);

      byte[] nameData, valueData = null;
      int nameLength, valueLength;

      nameData = Encoding.UTF8.GetBytes(notationName);
      nameLength = System.Math.Min(nameData.Length, 0xFF);

      valueData = Encoding.UTF8.GetBytes(notationValue);
      valueLength = System.Math.Min(valueData.Length, 0xFF);

      // name length
      os.WriteByte((byte)(nameLength >> 8));
      os.WriteByte((byte)(nameLength >> 0));

      // value length
      os.WriteByte((byte)(valueLength >> 8));
      os.WriteByte((byte)(valueLength >> 0));

      // name
      os.Write(nameData, 0, nameLength);

      // value
      os.Write(valueData, 0, valueLength);

      return os.ToArray();
    }

    public bool IsHumanReadable
    {
      get { return data[0] == (byte)0x80; }
    }

    public string GetNotationName()
    {
      int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
      int namePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength;

      return Encoding.UTF8.GetString(data, namePos, nameLength);
    }

    public string GetNotationValue()
    {
      int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
      int valueLength = ((data[HeaderFlagLength + HeaderNameLength] << 8) + (data[HeaderFlagLength + HeaderNameLength + 1] << 0));
      int valuePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength + nameLength;

      return Encoding.UTF8.GetString(data, valuePos, valueLength);
    }

    public byte[] GetNotationValueBytes()
    {
      int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
      int valueLength = ((data[HeaderFlagLength + HeaderNameLength] << 8) + (data[HeaderFlagLength + HeaderNameLength + 1] << 0));
      int valuePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength + nameLength;

      byte[] bytes = new byte[valueLength];
      Array.Copy(data, valuePos, bytes, 0, valueLength);
      return bytes;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.