sábado, 18 de julho de 2015

SignDoc

http://www.flexdocs.com.br/guiaNFe/gerarNFe.emi.emitente2G.html

   public Mensagem SignDoc(string ArquivoXmlEntrada, string ArquivoXmlSaida, string pUri, X509Certificate2 pCertificado)
        {
            try
            {
                X509Certificate2 certificado = SearchByName(pCertificado.Subject.ToString());
                if (certificado != null)
                {
                    string Algoritmo = certificado.GetKeyAlgorithm().ToString();
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.PreserveWhitespace = true;
                    xmlDoc.Load(ArquivoXmlEntrada);
      
                    int qtdeRefUri = xmlDoc.GetElementsByTagName(pUri).Count;
                    if (qtdeRefUri == 0)
                    {
                        return new Mensagem { Description = String.Format(RS.Resource.Msg_Assinatura_NaoExiste,pUri), Codigo = 1, Error = false };
                    }
                    else
                    {
                        if (qtdeRefUri > 1)
                        {
                            return new Mensagem { Description = String.Format(RS.Resource.Msg_Tag_NaoUnica,pUri), Codigo = 1, Error = false };
                        }
                        else
                        {
                            try
                            {

                                SignedXml signedXml = new SignedXml(xmlDoc);
                                signedXml.SigningKey = pCertificado.PrivateKey;

                                Reference reference = new Reference();
                                XmlAttributeCollection _Uri = xmlDoc.GetElementsByTagName(pUri).Item(0).Attributes;
                                foreach (XmlAttribute _atributo in _Uri)
                                {
                                    if (_atributo.Name == "Id")
                                        reference.Uri = "#" + _atributo.InnerText;
                                }

                                XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
                                reference.AddTransform(env);
                                XmlDsigC14NTransform c14 = new XmlDsigC14NTransform();
                                reference.AddTransform(c14);
                                signedXml.AddReference(reference);

                                KeyInfo keyInfo = new KeyInfo();
                                keyInfo.AddClause(new KeyInfoX509Data(pCertificado));
                                signedXml.KeyInfo = keyInfo;
                                signedXml.ComputeSignature();
                                XmlElement xmlDigitalSignature = signedXml.GetXml();

                                xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
                                XmlDocument XMLDoc = new XmlDocument();
                                XMLDoc.PreserveWhitespace = false;
                                XMLDoc = xmlDoc;

                                xmlDoc.Save(ArquivoXmlSaida);
                            }
                            catch (Exception ex)
                            {
                                return new Mensagem { Description = ex.Message, Codigo = -6, Error = true };
                            }
                        }
                    }
                }
                return new Mensagem { Description = RS.Resource.Msg_Sucesso, Codigo = 1, Error = false };
            }
            catch (System.Exception ex)
            {
                return new Mensagem { Description = ex.Message, Codigo = -1, Error = true };
            }
            finally
            {

            }

        }


Postar um comentário