public byte[] SignHash(string hexhash, string password)
        {
            byte[]      hash  = StringToByteArray(hexhash);
            Pkcs12Store store = new Pkcs12Store(getCertificate(), password.ToCharArray());
            String      alias = "";

            foreach (string al in store.Aliases)
            {
                if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
                {
                    alias = al;
                    break;
                }
            }
            AsymmetricKeyEntry pk = store.GetKey(alias);

            X509CertificateEntry[] chain = store.GetCertificateChain(alias);
            List <Org.BouncyCastle.X509.X509Certificate> c = new List <Org.BouncyCastle.X509.X509Certificate>();

            foreach (X509CertificateEntry en in chain)
            {
                c.Add(en.Certificate);
            }
            PrivateKeySignature signature = new PrivateKeySignature(pk.Key, "SHA256");
            String   hashAlgorithm        = signature.GetHashAlgorithm();
            PdfPKCS7 sgn         = new PdfPKCS7(null, c, hashAlgorithm, false);
            DateTime signingTime = DateTime.Now;

            byte[] sh           = sgn.getAuthenticatedAttributeBytes(hash, null, null, CryptoStandard.CMS);
            byte[] extSignature = signature.Sign(sh);
            sgn.SetExternalDigest(extSignature, null, signature.GetEncryptionAlgorithm());
            return(sgn.GetEncodedPKCS7(hash, null, null, null, CryptoStandard.CMS));
        }
示例#2
0
            public byte[] Sign(Stream data)
            {
                PrivateKeySignature signature = new PrivateKeySignature(pk.Key, "SHA256");
                String   hashAlgorithm        = signature.GetHashAlgorithm();
                PdfPKCS7 sgn = new PdfPKCS7(null, chain, hashAlgorithm, false);

                byte[]   hash        = DigestAlgorithms.Digest(data, hashAlgorithm);
                DateTime signingTime = DateTime.Now;

                byte[] sh           = sgn.getAuthenticatedAttributeBytes(hash, signingTime, null, null, CryptoStandard.CMS);
                byte[] extSignature = signature.Sign(sh);
                sgn.SetExternalDigest(extSignature, null, signature.GetEncryptionAlgorithm());
                return(sgn.GetEncodedPKCS7(hash, signingTime, null, null, null, CryptoStandard.CMS));
            }
示例#3
0
 internal static byte[] SignDocBytesHash(byte[] docBytesHash, ICipherParameters pk, X509Certificate[] chain
                                         )
 {
     if (pk == null || chain == null)
     {
         return(null);
     }
     byte[] signatureContent = null;
     try {
         PdfPKCS7 pkcs7      = new PdfPKCS7(null, chain, HASH_ALGORITHM, false);
         byte[]   attributes = pkcs7.GetAuthenticatedAttributeBytes(docBytesHash, null, null, PdfSigner.CryptoStandard
                                                                    .CMS);
         PrivateKeySignature signature = new PrivateKeySignature(pk, HASH_ALGORITHM);
         byte[] attrSign = signature.Sign(attributes);
         pkcs7.SetExternalDigest(attrSign, null, signature.GetEncryptionAlgorithm());
         signatureContent = pkcs7.GetEncodedPKCS7(docBytesHash, null, null, null, PdfSigner.CryptoStandard.CMS);
     }
     catch (GeneralSecurityException) {
     }
     // dummy catch clause
     return(signatureContent);
 }
示例#4
0
            public byte[] Sign(Stream inputStream)
            {
                try
                {
                    PrivateKeySignature signature = new PrivateKeySignature(pk, "SHA256");
                    String hashAlgorithm          = signature.GetHashAlgorithm();

                    PdfPKCS7 sgn  = new PdfPKCS7(null, chain, hashAlgorithm, false);
                    byte[]   hash = DigestAlgorithms.Digest(inputStream, hashAlgorithm);
                    byte[]   sh   = sgn.GetAuthenticatedAttributeBytes(hash, PdfSigner.CryptoStandard.CMS,
                                                                       null, null);
                    byte[] extSignature = signature.Sign(sh);
                    sgn.SetExternalDigest(extSignature, null, signature.GetEncryptionAlgorithm());

                    return(sgn.GetEncodedPKCS7(hash, PdfSigner.CryptoStandard.CMS, null,
                                               null, null));
                }
                catch (IOException ioe)
                {
                    throw new Exception(ioe.Message);
                }
            }