예제 #1
0
        /// <summary>
        /// Generates the transportable public key out of the properties
        /// in this.
        /// </summary>
        /// <remarks>No remarks</remarks>
        /// <returns>Returns a byte array containing the openpgp encoded
        /// representation of the transportable public key.</returns>
        public byte[] Generate()
        {
            byte[] bPrimaryKey = this.PrimaryKey.Generate();

            //Revocation signatures
            int         iLength       = 0;
            IEnumerator ieRevocations = this.RevocationSignatures.GetEnumerator();

            while (ieRevocations.MoveNext())
            {
                if (ieRevocations.Current is SignaturePacket)
                {
                    iLength += ((SignaturePacket)ieRevocations.Current).Generate().Length;
                }
            }
            byte[] bRevocations = new byte[iLength];
            ieRevocations = this.RevocationSignatures.GetEnumerator();
            int iPosition = 0;

            while (ieRevocations.MoveNext())
            {
                if (ieRevocations.Current is SignaturePacket)
                {
                    byte[] bRev = ((SignaturePacket)ieRevocations.Current).Generate();
                    Array.Copy(bRev, 0, bRevocations, iPosition, bRev.Length);
                    iPosition += bRev.Length;
                }
            }

            // Revoker keys
            iLength = 0;
            IEnumerator ieRevoker = this.RevocationKeys.GetEnumerator();

            while (ieRevoker.MoveNext())
            {
                if (ieRevoker.Current is SignaturePacket)
                {
                    iLength += ((SignaturePacket)ieRevoker.Current).Generate().Length;
                }
            }

            byte[] bRevoker = new byte[iLength];
            ieRevoker = this.RevocationKeys.GetEnumerator();
            iPosition = 0;
            while (ieRevoker.MoveNext())
            {
                if (ieRevoker.Current is SignaturePacket)
                {
                    byte[] bRev = ((SignaturePacket)ieRevoker.Current).Generate();
                    Array.Copy(bRev, 0, bRevoker, iPosition, bRev.Length);
                    iPosition += bRev.Length;
                }
            }

            //Certificates
            iLength = 0;
            IEnumerator ieCertificates = this.Certifications.GetEnumerator();

            while (ieCertificates.MoveNext())
            {
                if (ieCertificates.Current is CertifiedUserID)
                {
                    CertifiedUserID cuiID = (CertifiedUserID)ieCertificates.Current;
                    iLength += cuiID.Generate().Length;
                }
            }
            byte[] bCertificates = new byte[iLength];
            iPosition      = 0;
            ieCertificates = this.Certifications.GetEnumerator();
            while (ieCertificates.MoveNext())
            {
                if (ieCertificates.Current is CertifiedUserID)
                {
                    CertifiedUserID cuiID = (CertifiedUserID)ieCertificates.Current;
                    byte[]          bCert = cuiID.Generate();
                    Array.Copy(bCert, 0, bCertificates, iPosition, bCert.Length);
                    iPosition += bCert.Length;
                }
            }

            //SubKeys
            iLength = 0;
            IEnumerator ieSubkeys = this.SubKeys.GetEnumerator();

            while (ieSubkeys.MoveNext())
            {
                if (ieSubkeys.Current is CertifiedPublicSubkey)
                {
                    CertifiedPublicSubkey cpsKey = (CertifiedPublicSubkey)ieSubkeys.Current;
                    iLength += cpsKey.Generate().Length;
                }
            }
            byte[] bSubkeys = new byte[iLength];
            iPosition = 0;
            ieSubkeys = this.SubKeys.GetEnumerator();
            while (ieSubkeys.MoveNext())
            {
                if (ieSubkeys.Current is CertifiedPublicSubkey)
                {
                    CertifiedPublicSubkey cpsKey = (CertifiedPublicSubkey)ieSubkeys.Current;
                    byte[] bKey = cpsKey.Generate();
                    Array.Copy(bKey, 0, bSubkeys, iPosition, bKey.Length);
                    iPosition += bKey.Length;
                }
            }

            byte[] bData = new byte[bPrimaryKey.Length + bRevocations.Length +
                                    bRevoker.Length + bCertificates.Length + bSubkeys.Length];
            iPosition = 0;
            Array.Copy(bPrimaryKey, bData, bPrimaryKey.Length);
            iPosition = bPrimaryKey.Length;
            Array.Copy(bRevocations, 0, bData, iPosition, bRevocations.Length);
            iPosition += bRevocations.Length;
            Array.Copy(bRevoker, 0, bData, iPosition, bRevoker.Length);
            iPosition += bRevoker.Length;
            Array.Copy(bCertificates, 0, bData, iPosition, bCertificates.Length);
            iPosition += bCertificates.Length;
            Array.Copy(bSubkeys, 0, bData, iPosition, bSubkeys.Length);

            return(bData);
        }