Пример #1
0
        /**
         * Return the byte encoding of the signature section.
         * @return uninterpreted signature bytes.
         */
        public byte[] GetSignatureBytes()
        {
            if (signatureEncoding != null)
            {
                return((byte[])signatureEncoding.Clone());
            }

            MemoryStream     bOut  = new MemoryStream();
            BcpgOutputStream bcOut = new BcpgOutputStream(bOut);

            foreach (MPInteger sigObj in signature)
            {
                try
                {
                    bcOut.WriteObject(sigObj);
                }
                catch (IOException e)
                {
                    throw new Exception("internal error: " + e);
                }
            }

            return(bOut.ToArray());
        }
Пример #2
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteObjects(d, p, q, u);
 }
Пример #3
0
        /// <summary>
        /// Computes the hash value of the specified input stream using the specified
        /// hash algorithm, and signs the resulting hash value.
        /// </summary>
        /// <param name="inputStream">The input data for which to compute the hash.</param>
        /// <param name="rsaProvider">The RSA crypto service provider.</param>
        /// <param name="keyID">The unique key id of the public secret key pair.</param>
        /// <param name="hashAlgorithm">The hash algorithm to use to create the hash value.</param>
        /// <returns>The signature for the specified data.</returns>
        public byte[] SignData(Stream inputStream, RSACryptoServiceProvider rsaProvider, long keyID, Nequeo.Cryptography.HashcodeType hashAlgorithm = HashcodeType.SHA512)
        {
            MemoryStream output = null;

            Key.Bcpg.BcpgOutputStream pgpOutput = null;

            try
            {
                int ch;
                output = new MemoryStream();

                // Export the signer private key parameters.
                RSAParameters rsaPrivateKeySignerParam = rsaProvider.ExportParameters(true);
                Key.Crypto.Parameters.RsaPrivateCrtKeyParameters rsaPrivateKeySigner =
                    new Key.Crypto.Parameters.RsaPrivateCrtKeyParameters(
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.Modulus),
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.Exponent),
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.D),
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.P),
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.Q),
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.DP),
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.DQ),
                        new Key.Math.BigInteger(1, rsaPrivateKeySignerParam.InverseQ)
                        );

                // Get the private key.
                Key.Bcpg.OpenPgp.PgpPrivateKey privateKey = new Key.Bcpg.OpenPgp.PgpPrivateKey(rsaPrivateKeySigner, keyID);

                // Create a signature generator.
                Key.Bcpg.OpenPgp.PgpSignatureGenerator signatureGenerator =
                    new Key.Bcpg.OpenPgp.PgpSignatureGenerator(Key.Bcpg.PublicKeyAlgorithmTag.RsaGeneral, GetHashAlgorithm(hashAlgorithm));
                signatureGenerator.InitSign(Key.Bcpg.OpenPgp.PgpSignature.BinaryDocument, privateKey);

                // Create the output stream.
                pgpOutput = new Key.Bcpg.BcpgOutputStream(output);

                // Read the input stream.
                while ((ch = inputStream.ReadByte()) >= 0)
                {
                    // Update the generator.
                    signatureGenerator.Update((byte)ch);
                }

                // Write the hash to the output stream.
                Key.Bcpg.OpenPgp.PgpSignature signature = signatureGenerator.Generate();
                signature.Encode(pgpOutput);

                // Return the signed value.
                return(output.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (output != null)
                {
                    output.Close();
                }

                if (pgpOutput != null)
                {
                    pgpOutput.Close();
                }
            }
        }
Пример #4
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteObject(x);
 }
Пример #5
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(tag, contents, true);
 }
Пример #6
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.ModificationDetectionCode, digest, false);
 }
Пример #7
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.Marker, marker, true);
 }
Пример #8
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.PublicSubkey, GetEncodedContents(), true);
 }
Пример #9
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.Trust, levelAndTrustAmount, true);
 }
Пример #10
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteShort((short)val.BitLength);
     bcpgOut.Write(val.ToByteArrayUnsigned());
 }
Пример #11
0
 public abstract void Encode(BcpgOutputStream bcpgOut);
Пример #12
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.UserId, idData, true);
 }