PgpPublicKeyRing.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 » PgpPublicKeyRing.cs
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Utilities.Collections;

namespace Org.BouncyCastle.Bcpg.OpenPgp{
  /// <remarks>
  /// Class to hold a single master public key and its subkeys.
  /// <p>
  /// Often PGP keyring files consist of multiple master keys, if you are trying to process
  /// or construct one of these you should use the <c>PgpPublicKeyRingBundle</c> class.
  /// </p>
  /// </remarks>
  public class PgpPublicKeyRing
    : PgpKeyRing
    {
        private readonly ArrayList keys;

    public PgpPublicKeyRing(
            byte[] encoding)
            : this(new MemoryStream(encoding, false))
        {
        }

    internal PgpPublicKeyRing(
            ArrayList pubKeys)
        {
            this.keys = pubKeys;
        }

    public PgpPublicKeyRing(
            Stream inputStream)
        {
      this.keys = new ArrayList();

            BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream);

      PacketTag initialTag = bcpgInput.NextPacketTag();
            if (initialTag != PacketTag.PublicKey && initialTag != PacketTag.PublicSubkey)
            {
                throw new IOException("public key ring doesn't start with public key tag: "
          + "tag 0x" + ((int)initialTag).ToString("X"));
            }

      PublicKeyPacket pubPk = (PublicKeyPacket) bcpgInput.ReadPacket();;
      TrustPacket trustPk = ReadOptionalTrustPacket(bcpgInput);

            // direct signatures and revocations
      ArrayList keySigs = ReadSignaturesAndTrust(bcpgInput);

      ArrayList ids, idTrusts, idSigs;
      ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs);

      keys.Add(new PgpPublicKey(pubPk, trustPk, keySigs, ids, idTrusts, idSigs));


      // Read subkeys
      while (bcpgInput.NextPacketTag() == PacketTag.PublicSubkey)
            {
                PublicKeyPacket  pk = (PublicKeyPacket) bcpgInput.ReadPacket();
        TrustPacket kTrust = ReadOptionalTrustPacket(bcpgInput);

        // PGP 8 actually leaves out the signature.
        ArrayList sigList = ReadSignaturesAndTrust(bcpgInput);

        keys.Add(new PgpPublicKey(pk, kTrust, sigList));
            }
        }

    /// <summary>Return the first public key in the ring.</summary>
        public PgpPublicKey GetPublicKey()
        {
            return (PgpPublicKey) keys[0];
        }

    /// <summary>Return the public key referred to by the passed in key ID if it is present.</summary>
        public PgpPublicKey GetPublicKey(
            long keyId)
        {
      foreach (PgpPublicKey k in keys)
      {
        if (keyId == k.KeyId)
                {
                    return k;
                }
            }

      return null;
        }

    /// <summary>Allows enumeration of all the public keys.</summary>
    /// <returns>An <c>IEnumerable</c> of <c>PgpPublicKey</c> objects.</returns>
        public IEnumerable GetPublicKeys()
        {
            return new EnumerableProxy(keys);
        }

    public byte[] GetEncoded()
        {
            MemoryStream bOut = new MemoryStream();

      Encode(bOut);

      return bOut.ToArray();
        }

    public void Encode(
            Stream outStr)
        {
      if (outStr == null)
        throw new ArgumentNullException("outStr");

      foreach (PgpPublicKey k in keys)
      {
        k.Encode(outStr);
            }
        }

    /// <summary>
    /// Returns a new key ring with the public key passed in either added or
    /// replacing an existing one.
    /// </summary>
    /// <param name="pubRing">The public key ring to be modified.</param>
    /// <param name="pubKey">The public key to be inserted.</param>
    /// <returns>A new <c>PgpPublicKeyRing</c></returns>
        public static PgpPublicKeyRing InsertPublicKey(
            PgpPublicKeyRing  pubRing,
            PgpPublicKey    pubKey)
        {
            ArrayList keys = new ArrayList(pubRing.keys);
            bool found = false;
      bool masterFound = false;

      for (int i = 0; i != keys.Count; i++)
            {
                PgpPublicKey key = (PgpPublicKey) keys[i];

        if (key.KeyId == pubKey.KeyId)
                {
                    found = true;
                    keys[i] = pubKey;
                }
        if (key.IsMasterKey)
        {
          masterFound = true;
        }
      }

      if (!found)
            {
        if (pubKey.IsMasterKey)
        {
          if (masterFound)
            throw new ArgumentException("cannot add a master key to a ring that already has one");

          keys.Insert(0, pubKey);
        }
        else
        {
          keys.Add(pubKey);
        }
      }

      return new PgpPublicKeyRing(keys);
        }

    /// <summary>Returns a new key ring with the public key passed in removed from the key ring.</summary>
    /// <param name="pubRing">The public key ring to be modified.</param>
    /// <param name="pubKey">The public key to be removed.</param>
    /// <returns>A new <c>PgpPublicKeyRing</c>, or null if pubKey is not found.</returns>
        public static PgpPublicKeyRing RemovePublicKey(
            PgpPublicKeyRing  pubRing,
            PgpPublicKey    pubKey)
        {
            ArrayList keys = new ArrayList(pubRing.keys);
            bool found = false;

      for (int i = 0; i < keys.Count; i++)
            {
                PgpPublicKey key = (PgpPublicKey) keys[i];

        if (key.KeyId == pubKey.KeyId)
                {
                    found = true;
                    keys.RemoveAt(i);
                }
            }

      return found ? new PgpPublicKeyRing(keys) : null;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.